aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

Unable to use with vite

Open klarkc opened this issue 3 years ago • 16 comments

Describe the bug

It is not possible to import the sdk using vite (which uses rollup to bundle). It fails because it imports node-only dependencies into browser environment.

Your environment

Vite 2.3.7 (rollup bundler) with Node 15.14.0

SDK version number

@aws-sdk/[email protected]

Is the issue in the browser/Node.js/ReactNative?

Browser

Steps to reproduce

https://github.com/klarkc/aws-sdk-js-v3

cd tests/vite-project
yarn install
yarn build

Observed behavior

Error: 'request' is not exported by __vite-browser-external, imported by node_modules/@aws-sdk/credential-provider-imds/dist/es/remoteProvider/httpRequest.js

Looking in the file, I see why, it imports request from http that does not exists in browsers. It seems that other packages have the same problem (I've tested with @aws-sdk/client-cognito-identity, same result).

Expected behavior

Should build without errors

Additional context

Related issue: https://github.com/aws-amplify/amplify-js/issues/7499 Vite wont fix: https://github.com/vitejs/vite/issues/1374#issuecomment-754820938

klarkc avatar Jun 10 '21 23:06 klarkc

Related: https://github.com/aws/aws-sdk-js-v3/issues/1164#issuecomment-806157547

trivikr avatar Jun 30 '21 17:06 trivikr

Related: https://github.com/aws/aws-sdk-js/issues/3673

jiahao-c avatar Jan 04 '22 04:01 jiahao-c

Is there any updates? I run into the same issue.

sinkersan avatar Feb 15 '22 11:02 sinkersan

@klarkc did you solve this?

mdhelaluddin-ctg-bd avatar Mar 14 '22 06:03 mdhelaluddin-ctg-bd

@mdhelaluddin-ctg-bd in https://github.com/aws/aws-sdk-js-v3/pull/2491 I solved only the occurrences in @aws-sdk/credential-provider-cognito-identity and @aws-sdk/client-cognito-identity packages

klarkc avatar Mar 14 '22 14:03 klarkc

Some the techniques used to get AWS Amplify working with vite may be applicable.

dmost714 avatar Mar 18 '22 16:03 dmost714

Workaround for a vue 2 project. Add this to your vite.config.js:

export default defineConfig({
	resolve: {
  		alias: {
			"./runtimeConfig": "./runtimeConfig.browser"
		}
	}
})

And this to index.html:

<script>
	window.global = window;
</script>

(Via https://github.com/aws-amplify/amplify-js/issues/9639#issuecomment-1049158644)

FlorianWendelborn avatar May 31 '22 18:05 FlorianWendelborn

I can't reproduce this problem with vite 2.9.9 and "@aws-sdk/credential-provider-cognito-identity": "^3.118.0", the latest as of today.

The distributable file structure has changed since the original issue was reported. Most browser/node incompatibility issues can be resolved by ensuring any bundlers involved are following the runtimeConfig.browser alias specified in each @aws-sdk/***-client package's package.json.browser entry.

kuhe avatar Jun 27 '22 16:06 kuhe

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

github-actions[bot] avatar Jul 05 '22 00:07 github-actions[bot]

I have this issue with vite 2.9.11 and amazon-location-helpers 1.1.1 which depends on AWS Amplify and Cognito. Specifically I'm using Vite as configured by the Laravel framework - happy to provide a repro if helpful.

dwightwatson avatar Jul 05 '22 01:07 dwightwatson

What is the status on this? I'm seeing similar issues with Vite 3.0.0 and @aws-sdk ^3.118.0 packages.

[ERROR] No matching export in "../../node_modules/@aws-sdk/credential-provider-web-identity/dist-es/index.js" for import "fromTokenFile"

    ../../node_modules/@aws-sdk/credential-provider-ini/dist-es/resolveWebIdentityCredentials.js:2:9:
      2 │ import { fromTokenFile } from "@aws-sdk/credential-provider-web-...
        ╵          ~~~~~~~~~~~~~

✘ [ERROR] No matching export in "../../node_modules/@aws-sdk/credential-provider-web-identity/dist-es/index.js" for import "fromTokenFile"

    ../../node_modules/@aws-sdk/credential-provider-node/dist-es/defaultProvider.js:6:9:
      6 │ import { fromTokenFile } from "@aws-sdk/credential-provider-web-...
        ╵          ~~~~~~~~~~~~~

✘ [ERROR] No matching export in "../../node_modules/@aws-sdk/credential-provider-web-identity/dist-es/index.js" for import "fromTokenFile"

    ../../node_modules/@aws-sdk/credential-providers/dist-es/fromTokenFile.js:3:9:
      3 │ import { fromTokenFile as _fromTokenFile, } from "@aws-sdk/crede...

MagnusHJensen avatar Aug 01 '22 13:08 MagnusHJensen

@dwightwatson @MagnusHJensen

could you provide a minimal reproduction repository?

kuhe avatar Aug 09 '22 02:08 kuhe

Here's an example from my use-case which uses Vite and AWS's amazon-location-helpers package: https://github.com/dwightwatson/vite

dwightwatson avatar Aug 09 '22 22:08 dwightwatson

There is a workaround in amplify docs.

https://ui.docs.amplify.aws/react/getting-started/troubleshooting#vite

takayamaki avatar Aug 10 '22 04:08 takayamaki

@takayamaki it's great that a workaround exists, but it's quite a demand that users pollute their globals just to get some random library to work. The library shouldn't be written in such a way that makes this necessary

FlorianWendelborn avatar Aug 16 '22 21:08 FlorianWendelborn

What is the status on this? I'm seeing similar issues with Vite 3.0.0 and @aws-sdk ^3.118.0 packages.

[ERROR] No matching export in "../../node_modules/@aws-sdk/credential-provider-web-identity/dist-es/index.js" for import "fromTokenFile"

    ../../node_modules/@aws-sdk/credential-provider-ini/dist-es/resolveWebIdentityCredentials.js:2:9:
      2 │ import { fromTokenFile } from "@aws-sdk/credential-provider-web-...
        ╵          ~~~~~~~~~~~~~

✘ [ERROR] No matching export in "../../node_modules/@aws-sdk/credential-provider-web-identity/dist-es/index.js" for import "fromTokenFile"

    ../../node_modules/@aws-sdk/credential-provider-node/dist-es/defaultProvider.js:6:9:
      6 │ import { fromTokenFile } from "@aws-sdk/credential-provider-web-...
        ╵          ~~~~~~~~~~~~~

✘ [ERROR] No matching export in "../../node_modules/@aws-sdk/credential-provider-web-identity/dist-es/index.js" for import "fromTokenFile"

    ../../node_modules/@aws-sdk/credential-providers/dist-es/fromTokenFile.js:3:9:
      3 │ import { fromTokenFile as _fromTokenFile, } from "@aws-sdk/crede...

@MagnusHJensen Did you ever find a solution to this?

jhubbardsf avatar Sep 14 '22 06:09 jhubbardsf

There is no error in vite 3.2.4 dev/build command with @aws-sdk/[email protected]. I followed Getting Started Guide from vite.

$ yarn create vite
...
success Installed "[email protected]" with binaries:
      - create-vite
      - cva
✔ Project name: … vite-project
✔ Select a framework: › Vanilla
✔ Select a variant: › TypeScript

Scaffolding project in /Users/trivikr/workspace/vite-project...
...

$ cd vite-project

$ yarn vite --version
vite/3.2.4 darwin-x64 node-v16.18.0

$ yarn add @aws-sdk/[email protected] --exact
...

# Add import in src/main.ts
$ sed -i "1s|^|import '@aws-sdk/credential-provider-cognito-identity';\n|" src/main.ts

$ yarn dev
... no issues

$ yarn build
...
vite v3.2.4 building for production...
✓ 360 modules transformed.
dist/assets/typescript.f6ead1af.svg   1.40 KiB
dist/index.html                       0.44 KiB
dist/assets/index.acb3e620.js         1.40 KiB / gzip: 0.72 KiB
dist/assets/index.d5a34e39.css        1.19 KiB / gzip: 0.62 KiB
✨  Done in 2.90s.

trivikr avatar Nov 15 '22 18:11 trivikr

Is this issue still reproducible for you? If yes, can you provide repro steps?

trivikr avatar Nov 15 '22 18:11 trivikr

@trivikr did an upgrade to vite 2.9.15 and @aws-sdk/credential-provider-cognito-identity to 3.210.0, and it's working normally now: klarkc/aws-sdk-js-v3@c18235998024eb03e75e34e9bf47003ab067c541

klarkc avatar Nov 15 '22 19:11 klarkc

Vite 3.2.4, @aws-sdk/credential-provider-cognito-identity 3.211.0 and I still can't complete a build with Vite. Though it looks like the error might have moved to @aws-sdk/client-cloudwatch-logs now?

'request' is not exported by __vite-browser-external, imported by node_modules/@aws-sdk/client-cloudwatch-logs/node_modules/@aws-sdk/credential-provider-imds/dist/es/remoteProvider/httpRequest.js
file: /Users/dwight/Sites/app/node_modules/@aws-sdk/client-cloudwatch-logs/node_modules/@aws-sdk/credential-provider-imds/dist/es/remoteProvider/httpRequest.js:4:9
2: import { ProviderError } from "@aws-sdk/property-provider";
3: import { Buffer } from "buffer";
4: import { request } from "http";
            ^
5: /**
6:  * @internal
error during build:
Error: 'request' is not exported by __vite-browser-external, imported by node_modules/@aws-sdk/client-cloudwatch-logs/node_modules/@aws-sdk/credential-provider-imds/dist/es/remoteProvider/httpRequest.js
    at error (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:1858:30)
    at Module.error (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:12429:16)
    at Module.traceVariable (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:12788:29)
    at ModuleScope.findVariable (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:11440:39)
    at FunctionScope.findVariable (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
    at ChildScope.findVariable (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
    at FunctionScope.findVariable (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
    at ChildScope.findVariable (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:6372:38)
    at Identifier.bind (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:7439:40)
    at CallExpression.bind (file:///Users/dwight/Sites/app/node_modules/rollup/dist/es/shared/rollup.js:5269:23)

dwightwatson avatar Nov 15 '22 23:11 dwightwatson

@dwightwatson can you make sure that all sub-dependencies are also up-to-date? The easiest way is to do that is to uninstall the package and then reinstall it at the desired version

FlorianWendelborn avatar Nov 17 '22 09:11 FlorianWendelborn

Yeah, tried that. Perhaps my issue is that my entry point is the specific amazon-location-helpers library instead.

yarn remove amazon-location-helpers
yarn add amazon-location-helpers --dev
yarn run build

dwightwatson avatar Nov 17 '22 22:11 dwightwatson

Vite 3.2.4, @aws-sdk/credential-provider-cognito-identity 3.211.0 and I still can't complete a build with Vite. Though it looks like the error might have moved to @aws-sdk/client-cloudwatch-logs now?

@dwightwatson I'm not able to repro it with @aws-sdk/[email protected]. Can you provide the steps you're following, or a repro repo?

$ yarn create vite
...
success Installed "[email protected]" with binaries:
      - create-vite
      - cva
✔ Project name: … vite-project
✔ Select a framework: › Vanilla
✔ Select a variant: › TypeScript

Scaffolding project in /Users/trivikr/workspace/vite-project...
...

$ cd vite-project

$ yarn vite --version
vite/3.2.4 darwin-x64 node-v16.18.0

$ yarn add @aws-sdk/[email protected] --exact
...
├─ [email protected]
└─ [email protected]
Done in 4.49s.

# Add import in src/main.ts
$ sed -i "1s|^|import '@aws-sdk/client-cloudwatch-logs';\n|" src/main.ts

$ yarn dev
... no issues in build or on opening webpage on http://localhost:5173/

$ yarn build
...
vite v3.2.4 building for production...
✓ 360 modules transformed.
dist/assets/typescript.f6ead1af.svg   1.40 KiB
dist/index.html                       0.44 KiB
dist/assets/index.acb3e620.js         1.40 KiB / gzip: 0.72 KiB
dist/assets/index.d5a34e39.css        1.19 KiB / gzip: 0.62 KiB
✨  Done in 2.90s.

trivikr avatar Nov 18 '22 15:11 trivikr

I've got a repro repo here: https://github.com/dwightwatson/vite which fails when running yarn run build

dwightwatson avatar Nov 23 '22 00:11 dwightwatson

Any news on this issue?

alex-ironside avatar Dec 29 '22 12:12 alex-ironside

Any news on this? I can't use AWS-sdk/credentials-provider with sveltekit 1.0 and vite v4.

The error is:

the requested module '/node_modules/.vite/deps/@aws-sdk credential-providers doesn't export a function like "fromEnv"

But this is written and it seems to be an issue by the credentials-provider

Amplitude88 avatar Feb 06 '23 23:02 Amplitude88

Any news on this? I can't use AWS-sdk/credentials-provider with sveltekit 1.0 and vite v4.

The error is:

the requested module '/node_modules/.vite/deps/@aws-sdk credential-providers doesn't export a function like "fromEnv"

But this is written and it seems to be an issue by the credentials-provider

I have the same issue with Vite 4 and the aws-sdk/credentials-provider

kylemichaelreaves avatar Feb 11 '23 02:02 kylemichaelreaves

I've got a repro repo here: dwightwatson/vite

This issue is not with the direct import of AWS SDK for JavaScript (v3). I see that you'd created an issue with amazon-location-samples which was closed because of inactivity https://github.com/aws-samples/amazon-location-samples/issues/193

This issue is discussed in Amplify in https://github.com/aws-amplify/amplify-js/issues/9639, and the solution is to update your vite config as follows:

  resolve: {
    alias: {
      "./runtimeConfig": "./runtimeConfig.browser",
    },
  },

Any news on this? I can't use AWS-sdk/credentials-provider with sveltekit 1.0 and vite v4.

@Amplitude88 Is there a repro I can look at?

I have the same issue with Vite 4 and the aws-sdk/credentials-provider

@kylemichaelreaves Do you have a repro I can look at?

trivikr avatar Feb 15 '23 15:02 trivikr

There is no error in vite 4.1.0 dev/build command with @aws-sdk/[email protected]. I followed Getting Started Guide from vite.

$ yarn create vite
...
success Installed "[email protected]" with binaries:
      - create-vite
      - cva
✔ Project name: … vite-project
✔ Select a framework: › Vanilla
✔ Select a variant: › TypeScript

Scaffolding project in /Users/trivikr/workspace/vite-project...
...

$ cd vite-project

$ yarn set version stable 
➤ YN0000: Retrieving https://repo.yarnpkg.com/3.4.1/packages/yarnpkg-cli/bin/yarn.js
➤ YN0000: Saving the new release in .yarn/releases/yarn-3.4.1.cjs
➤ YN0000: Done in 0s 407ms

$ yarn

$ yarn vite --version
vite/4.1.1 darwin-arm64 node-v16.19.0

$ yarn add @aws-sdk/[email protected] --exact
...

# Add import in src/main.ts
$ sed -i "1s|^|import '@aws-sdk/credential-provider-cognito-identity';\n|" src/main.ts

$ yarn dev
... no issues

$ yarn build
...
vite v4.1.1 building for production...
✓ 6 modules transformed.
dist/index.html                      0.45 kB
dist/assets/typescript-f6ead1af.svg  1.44 kB
dist/assets/index-3443e464.css       1.24 kB │ gzip: 0.64 kB
dist/assets/index-5f8bed07.js        1.44 kB │ gzip: 0.74 kB

trivikr avatar Feb 15 '23 15:02 trivikr

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

github-actions[bot] avatar Feb 23 '23 00:02 github-actions[bot]