Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-sdk-js doesn't work on sveltekit. #4530

Open
higuuu opened this issue Nov 21, 2023 · 4 comments
Open

aws-sdk-js doesn't work on sveltekit. #4530

higuuu opened this issue Nov 21, 2023 · 4 comments
Assignees
Labels
bug This issue is a bug. investigating Issue has been looked at and needs deep dive work by OSDS. p2 This is a standard priority issue

Comments

@higuuu
Copy link

higuuu commented Nov 21, 2023

Describe the bug

I want to use this sdk on sceltekit.
So I installed this sdk and import .svelte file.
But it doesn't work.

A error is appeared after I build and run it.


TypeError: e is not a constructor

スクリーンショット 2023-11-13 22 00 13

However, it runs correctly only debug in 'npm run dev`.
I think it's related to Vite.

I tried that it's written by this comment but it doesn't work.
#3673 (comment)

Expected Behavior

If I import aws-sdk, I can use aws-sdk on sveltekit.

Current Behavior

A error is appeared after I build and run it.
However, it runs only debug by 'npm run dev`.


TypeError: e is not a constructor

スクリーンショット 2023-11-13 22 00 13

However, it runs correctly only debug in 'npm run dev`.
I think it's related to Vite.

Reproduction Steps

  1. create sveltekit project

node -v 18.16.0
create-svelte version 5.3.1

npm create svelte@latest [projectname]

  • Which Svelte app template? › SvelteKit demo app
  • Add type checking with TypeScript? › Yes, using TypeScript syntax

◆ Select additional options (use arrow keys/space bar)
│ ◼ Add ESLint for code linting
│ ◼ Add Prettier for code formatting
│ ◼ Add Playwright for browser testing
│ ◼ Add Vitest for unit testing

  1. import AWS-SDK
  • run npm install aws-sdk in the project

  • import the library in routes/+page.svelte file. like bellow.


<script>
	import Counter from './Counter.svelte';
	import welcome from '$lib/images/svelte-welcome.webp';
	import welcome_fallback from '$lib/images/svelte-welcome.png';
	// add bellow
	import AWS from 'aws-sdk'; 
	console.log(AWS.config) 
</script>

  1. Build & Preview
  • run npm run build

  • run npm run preview

  • After watch the localhost url , you can watch this issue

Possible Solution

I found this issue and I tried to change vite config and svelte config like bellow.
#3673

But it doesn't work.

svelte.config.js

import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/kit/vite';
/** @type {import('@sveltejs/kit').Config} */
const config = {
	preprocess: [vitePreprocess()],

	kit: {
		adapter: adapter({
			pages: 'build',
			assets: 'build',
			fallback: undefined,
			precompress: false,
			strict: true
		})
	},
	vite: {
    resolve: {
      alias: {
        './runtimeConfig': './runtimeConfig.browser',
      },
    },
  },
};

export default config;

vite.config.ts

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';

export default defineConfig({
	plugins: [
		sveltekit()
	],
	optimizeDeps: {
		esbuildOptions: {
				// Node.js global to browser globalThis
				define: {
						global: 'globalThis',
				},
		},
	},
  resolve: {
    alias: {
      './runtimeConfig': './runtimeConfig.browser' // fix production build
    }
  },
	test: {
		include: ['src/**/*.{test,spec}.{js,ts}']
	}
});

Additional Information/Context

No response

SDK version used

"aws-sdk": "^2.1501.0"

Environment details (OS name and version, etc.)

node -v 18.16.0, create-svelte version 5.3.1, iOS 14.0

@higuuu higuuu added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 21, 2023
@hax
Copy link

hax commented Dec 5, 2023

I guess it's commonjs/esm compat problem, try console.log(AWS.default.config) ?

@higuuu
Copy link
Author

higuuu commented Dec 7, 2023

I guess it's commonjs/esm compat problem, try console.log(AWS.default.config) ?

Maybe, is it console.log(AWS.config) ?
When I write console.log(AWS.default.config) , it shows ts error.
And, It doesn't work when I build and preview it.


Property 'default' does not exist on type 'typeof import("/Users/HiguchiShuya/v/test-svelte-kit/node_modules/aws-sdk/index")'.ts(

@RanVaknin
Copy link
Contributor

Hi,
This seems like an issue with ESM compatibility. The v3 SDK was published to specifically address concerns of better compatibility with bundlers and tools that make use of ESM.

I have tested this using v3 and it's working correctly.

Will look into the issue with v2.

Thanks,
Ran~

@RanVaknin RanVaknin added investigating Issue has been looked at and needs deep dive work by OSDS. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jan 22, 2024
@TomBrian
Copy link

For those still facing the issue. I had the same issue with Vite and Typescript. Switching from "aws-sdk" to @aws-sdk/client-s3" for S3Client and @aws-sdk/lib-storage for the upload method worked.

The "e is not a constructor" is an issue with the s3 import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. investigating Issue has been looked at and needs deep dive work by OSDS. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

4 participants