aws-sdk-js-v3: Unable to use with vite

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/credential-provider-cognito-identity@3.18.0

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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 30
  • Comments: 31 (8 by maintainers)

Most upvoted comments

@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

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)

@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

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

There is no error in vite 3.2.4 dev/build command with @aws-sdk/credential-provider-cognito-identity@3.210.0. I followed Getting Started Guide from vite.

$ yarn create vite
...
success Installed "create-vite@3.2.1" 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/credential-provider-cognito-identity@3.210.0 --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.

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