graphql-js: ESM named exports are not available with "type": "module"
A package.json top-level field type with a value of module should make modules load as ES modules.
When I used that with graphql, I expected this to work (it didn’t):
import { version } from "graphql"; // ❌
Importing from index.mjs works fine:
import { version } from "graphql/index.mjs"; // ✅
Shouldn’t ESM named exports work out of the box when Node’s package type is set to module?
Reproduce:
$ node -v
v14.6.0
$ echo '{ "name": "test", "version": "0.0.1", "type": "module" }' > package.json
$ npm i graphql
+ graphql@15.3.0
$ echo 'import { version } from "graphql"; console.log(version);' > test.js
$ node test.js
import { version } from "graphql"; console.log(version);
^^^^^^^
SyntaxError: The requested module 'graphql' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'graphql';
const { version } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
at async Loader.import (internal/modules/esm/loader.js:162:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 27
- Comments: 40
Commits related to this issue
- Update all dependencies - lock to npm 7 and node 15+ - update jest - fix graphql mjs issue see https://github.com/graphql/graphql-js/issues/2721#issuecomment-723008284 — committed to rvsia/insights-inventory-frontend by rvsia 3 years ago
- Update all dependencies (#1353) - lock to npm 7 and node 15+ - update jest - fix graphql mjs issue see https://github.com/graphql/graphql-js/issues/2721#issuecomment-723008284 — committed to RedHatInsights/insights-inventory-frontend by rvsia 3 years ago
- Update all dependencies (#1353) - lock to npm 7 and node 15+ - update jest - fix graphql mjs issue see https://github.com/graphql/graphql-js/issues/2721#issuecomment-723008284 — committed to RedHatInsights/insights-inventory-frontend-build by invalid-email-address 3 years ago
- Update all dependencies (#1353) - lock to npm 7 and node 15+ - update jest - fix graphql mjs issue see https://github.com/graphql/graphql-js/issues/2721#issuecomment-723008284 — committed to RedHatInsights/insights-inventory-frontend-build by invalid-email-address 3 years ago
- Fix webpack builds by disabling fully specified files See - https://webpack.js.org/configuration/module/#resolvefullyspecified - https://github.com/graphql/graphql-js/issues/2721#issuecomment-9299560... — committed to zarathustra323/base-cms by zarathustra323 3 years ago
The way I resolved is to add
to the [rules] section.
hi @kettanaito looks like webpack 5 doesn’t resolve it anymore. I’m getting this error:
Anyone got a solution to get graphql/amplify working with CRA5?
Upgrading
react-scriptsto5.0.0andwebpackto5.66.0got me to the same point. Currently getting the following message pretty much everywhereI encountered this problem when adding an amplify back-end to my application. The fix described by @flyandi (THANKS!) worked for me but now my build is slowed because webpack must transpile node_modules . Shouldn’t this issue be addressed in graphql imports/exports?
changes to my webpack.config.js …
Same problem here after upgrading to
react-scripts@5.0.0in a non-ejected CRA projectI have the same issue and using the solution from @flyandi did not work for me. Webpack 5.7.0.
With
main.jsconfig it works like this:@rudyhadoux I also had this with my ng v12 upgrade and I’m also using amplify. I did a manual install of graphql to get it up to v15.5 and all the errors went away.
npm install --save graphqldid the trick for meSo I think the issue lies with either graphql-tag or amplify using old deps, not with graphql itself
I confirm that it is now an issue with Angular 12, which had been upgraded to Webpack 5 and released yesterday. Angular doesn’t expose
webpack.config.jsby default, thus the proposed workaround is not applicable.In addition to that rule I think your resolve section also needs to look like this:
resolve: { extensions: ['.wasm', '.mjs', '.js', '.jsx', '.json'] },If using create-react-app, set up CRACO, and set your
craco.config.jsto:Verify that this is the first rule if you have multiple (It took me some time)
Where is the [rules] section? What do you mean by that? Please elaborate!
Hmm. I don’t use webpack/any build tool - just running typescript with node ES modules with the latest version of node.
Same MJS problem with Angular and AWS Amplify. @flyandi What is the file we must change please ?
It is a *.mjs files problem. https://github.com/graphql/graphql-js/issues/2721#issuecomment-723008284
I get a similar error but in
graphql-wsdependency when trying to run the project as an es moduleAny workaround apart from using commonjs?
This didn’t work for me either. Only thing that has worked is to add the extensions. That is, in index.mjs, I have to change
from './error'tofrom './error/index.js'It just bit me again. Quite a drag.
Hey, @javascriptlabs.
It seems that the error message implies the
graphqlimport hasn’t been resolved toindex.mjs, but to CommonJS build of thegraphqlmodule atnode_modules/graphql/index.js:I believe the issue is that while NodeJS treats your
index.jsas an ES module, it doesn’t resolve thegraphqlimport to its"module": "index.mjs"file. I’m searching NodeJS docs regarding this behavior, but cannot find it any mention that it would respect themoduleproperty of thepackage.jsonof the imported (third-party) module.The
modulebuild file will work when you bundle yourindex.jswith tools like webpack, as they respect themodulefield of GraphQL’spackage.json, and will includeindex.mjsinstead of its CommonJS module.