Cannot use amplify-js in browser environment (breaking vite / snowpack / esbuild)
See original GitHub issueDescribe the bug
The package aws-amplify is meant to be used in the browser, isnt it? Therefore it should not rely on node builtins, which sadly is the case and prevents the usage of this package in many js build tools like rollup, snowpack or vitejs.
The @aws-sdk package which is used by aws-amplify relies on various node build in modules like fs, process or http2, …
I described the issue also here (https://github.com/vitejs/vite/issues/1374) and Evan You the creator of vuejs and vite mentioned that webpack will drop support for node build ins in version five. Because webpack is used by tools such as create-react-app and vue-cli this will block many developers from using the amplify package soon I think!
To Reproduce Steps to reproduce the behavior:
- Download https://github.com/sem4phor/aws-cli-bug
- Run npm run dev (node required)
- See error in console
Expected behavior The package works in the browser and does not import any node build ins which are not available in browser environment.
Code Snippet https://github.com/sem4phor/aws-cli-bug
Screenshots
[plugin:vite:imports] Failed to resolve import "process". Does the file exist?
/Users/XXXXX/node_modules/@aws-sdk/util-user-agent-node/dist/es/index.js:1:22
1 | import process from "process";
| ^
2 | export function defaultUserAgent(packageName, packageVersion) {
3 | var engine = process.platform + "/" + process.version;
Error: Failed to resolve import "process". Does the file exist?
at TransformContext.error (C:\Users\XXXXX\node_modules\vite\dist\node\chunks\dep-262f13eb.js:59552:50)
at TransformContext.transform (C:\XXXXX\node_modules\vite\dist\node\chunks\dep-262f13eb.js:52957:30)
at async Object.transform (C:\Users\XXXXX\node_modules\vite\dist\node\chunks\dep-262f13eb.js:59742:30)
at async transformRequest (C:\Users\XXXXX\node_modules\vite\dist\node\chunks\dep-262f13eb.js:65977:29)
at async C:\Users\XXXXX\node_modules\vite\dist\node\chunks\dep-262f13eb.js:66068:32
What is Configured? Not relevant for this error.
- OS: Windows 10
- Browser: Chrome 87
- aws-amplify 3.3.13
Issue Analytics
- State:
- Created 3 years ago
- Reactions:79
- Comments:99 (11 by maintainers)
Top GitHub Comments
YES!!! I’ve been stuck on this for days!!! Thanks to @armenr a couple comments up I was able to get things running with just ‘aws-amplify’ (haven’t tried with the components library yet) All i actually needed was to add
Then i can use aws-amplify and actually have the project build as normal
For the record i’m using Vite, React, TS. Bootstrapped the project with the vite react template.
"vite": "2.6.12",
"aws-amplify": "4.3.3"
(now I’m just waiting for something else to go wrong as I progress through adding more functionality with amplify. Hope this helps you all that are struggling)
I found a workaround that can help for now. Eventually this will probably need to be fixed in the
@aws-sdk
Add a vite.confg.js file to the root of the project. You’ll need to add a new resolve.alias.
Then inside the index.html file add a script tag below the
<div id="app></div>
but above the module script importYou need to set
window.global
andexports
. If you don’t setwindow.global
it won’t work in development. If you don’t add invar exports = {}
then it will build, but will throw an error when you open it up on your web browser.I made a branch here you can take a look at too. https://github.com/ErikCH/amplify-js/tree/hotfix/error-with-vite/.github/actions/bundle-size-action/vite