amplify-cli: package.json with `"type": "module"` cause error when `amplify push`
Describe the bug
package.json with "type": "module"
be set cause the following error when I run amplify push
Must use import to load ES Module: /Users/x/Document/www/src/aws-exports.js
require() of ES modules is not supported.
require() of /Users/x/Document/www/src/aws-exports.js from /Users/x/.npm-global/lib/node_modules/@aws-amplify/cli/node_modules/amplify-frontend-javascript/lib/frontend-config-creator.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename aws-exports.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/x/Documents/www/package.json.
An error occurred during the push operation: Must use import to load ES Module: /Users/Document/www/aws-exports.js
require() of ES modules is not supported.
require() of /Users/x/Document/www/aws-exports.js from /Users/x/.npm-global/lib/node_modules/@aws-amplify/cli/node_modules/amplify-frontend-javascript/lib/frontend-config-creator.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename aws-exports.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/x/Documents/www/package.json.
Amplify CLI Version 4.30.0
node.js Version v14.11.0
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 9
- Comments: 37 (5 by maintainers)
The problem here is that Amplify CLI writes out an ES6 module: aws_exports.js.ejs but then attempts to require it as a CommonJS module: frontend-config-creator.js. It’s like trying to execute a C++ program with nodejs, these are different file formats. There’s documentation here: https://nodejs.org/api/packages.html
My suggestion to the Amplify team is to convert that template file into a CommonJS file and output with a
.cjs
extension. For the users of Amplify CLI I suggest replacingexport default awsmobile;
withmodule.exports = awsmobile;
perhaps with an Amplify hook.Same error for me trying to push sveltekit static build with amplify.
Hi @jhockett just to reinforce that this topic still not solved, and I agree with @yaquawa that this is not related to WebPack. I’m having the same issue trying to use the newly release SvelteKit with static distribution and console deploy.
It seems that as part of the “amplify push” process the aws-exports.js is required by “amplify-frontend-javascript/lib/frontend-config-creator.js”, and as the whole project is of type module it should have been imported.
I took a look at the code and found this piece where apparently there is a tentative to circumvent it, but it seems not working. Couldn’t deep dive on it to come with a PR.
Hey @bart and @armenr this fix has been released with the latest version of Amplify CLI, 10.2.3 🙂
None of the workarounds mentioned here work for our use case. We now have pre/post push/pull hooks to replace the type from module to commonjs before the operation and back to module afterwards to get the CI build working. There were several other places where buggy/non-existing ESM support in Amplify caused trouble after we upgraded to Angular 13/14 that only ships ES modules. I really hope proper ESM support is something the Amplify team will address, soon.
We started some academic exercises to deploy applications with Svelte-Kit in aws amplify, playing with Appi, storage … etc.
Installation method:
mkdir my-app cd my-app npm init svelte @ next npm install npm run dev - --open
I have bad news for you
For me, adding Amplify to my SvelteKit Project where type: is “module” was fairly easy… I did exactly what the error suggested… I watched it initialise and fail, then I renamed the aws-exports extension from
.js
to.cjs
Then I ran amplify init again… said ‘Yes’ to existing environment, selected ‘dev’ … and everything is now fine.
After any amplify commands I have to remove the .cjs and rename the .js to .cjs again.
It’s slightly annoying, but easily fixed.
Hi @rodgco, thanks for the detailed information. I’ll add this to our triaging project so this can get better visibility.
Sorry @yaquawa, I missed your comment somehow and never followed up.
@bart - no problem. I went down the Serverless Inc. framework route, as well as a few others.
In the end, the amount of value that Amplify provides (e.g. - PR preview environments, branch auto-detection + automatically provisioned backends per-branch, etc…) outweighed the cons/bugs/workarounds…so I’ve chosen to stick with Amplify.
IF you’re getting into the Serverless Inc. framework world of things, I’d highly encourage you to also check out SST and CDK too. Depending on your technology needs/objectives/business goals, those may be good to check out also. 😃
Thank you so much @armenr for sharing your hook scripts and diving deep into the topic. I already understood the hooks concept but after running into another Amplify issue (where I would have to find another workaround for) I decided to go the good old fashioned way using Serverless framework. In my opinion Amplify unfortunately is still not stable and flexible enough to get used in enterprise applications. But for sure this is just a personal opinion and I’m sure other devs would love to go this hook way. So again, thanks a lot for sharing!!
For @bart and ANYONE ELSE who hits this same problem:
Amplify (luckily) exposes some “entrypoints” for us to use to trigger hooks and scripts. See here: Amplify Docs - Command Hooks
In the root of your project (or in the folder within a monorepo) where your
amplify
folder is, you will see this folder structure:So, inside of that folder called
hooks
- you can drop off scripts (written either in JS or SH). I recommend straight shell to avoid even more pains in the ass and headaches with TypeScript, in case your project is TypeScript.Like this:
I needed to deal with this particular problem - the whole
Must use import to load ES Module
- in a way that is:jq
in this case)Therefore, I wrote two hook scripts.
If it helps instill confidence, I’ve been using these “in production” with my team + production customers/apps without any problems, for a few weeks now…
What do the hook scripts do?
The
pre-pull
script will:package.json.amplify-pre-pull
jq
to modify your project’s existing package.json (in place) by deleting thetype
key/value pairThe
post-pull
script will:package.json.amplify-pre-pull
file existsjq
made before pulling the Amplify projectIf you use
git
to check your repo before/after a pull (using my scripts), git won’t/shouldn’t report any changes topackage.json
😎NOTE: The script presumes/requires that you have
jq
installed on your development machine & in your CI/CD pipeline.NOTE
The first time you ADD these scripts to your amplify project, you have to IMMEDIATELY PUSH THEM TO GIT AND AMPLIFY
So, to “install” these scripts:
amplify/hooks/
directorygit add, git commit, git push
amplify push
THEN
amplify pull
OTHERWISE, if you add the scripts and then pull BEFORE pushing, amplify will ignore them/overwrite them/delete them.
Show me the pre/post hook scripts!
pre-pull.sh
post-pull.sh
@Karol-Perec and ALL:
A way to do this without introducing a custom
amplifyPull
script:or
This hook solutions sounds reasonable. Changing manually all the time would be a headache. Which hooks are we talking about here?
post-push.js
andpost-pull.js
maybe?Hi @jhockett I don’t think this is an “issue” of WebPack, some library like graphql-js already supports webpack 5 as of its latest version.
I believe this can be fixed by simply changing
aws-exports.js
toaws-exports.mjs
if the upstream(amplify-cli
) is using commonjs.By the way
amplify-cli
still depends on the old version of graphql-js so I have to replace the old one with the latest one manually.