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)
@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
:And this to
index.html
:(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
There is a workaround in amplify docs.
https://ui.docs.amplify.aws/react/getting-started/troubleshooting#vite
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.Some the techniques used to get AWS Amplify working with vite may be applicable.
Related: https://github.com/aws/aws-sdk-js-v3/issues/1164#issuecomment-806157547