Repository navigation
.mjs fails webpack build #493
Copy link
Copy link
Closed
Description
When using node-fetch with webpack v4.8.3, bundling fails with node-fetch v2.2. By default webpack will resolve the module entry from package.json. Webpack supports .mjs extension, it seems the problem is the ES6 imports from the built-in node_modules like http, which are being shipped in CommonJS format.
Is there a plan to support webpack 4 with the new .mjs format?
As a temporary workaround the issue can be solved by changing the mainFields entry to mainFields: ["main", "module"]. Note: this will affect all modules.
Error:
ERROR in ./node_modules/node-fetch/lib/index.mjs
1049:34-46 Can't import the named export 'STATUS_CODES' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
1142:16-21 Can't import the named export 'parse' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
1145:16-21 Can't import the named export 'parse' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
1149:15-20 Can't import the named export 'parse' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
1194:9-15 Can't import the named export 'format' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
1352:51-58 Can't import the named export 'resolve' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
1413:27-38 Can't import the named export 'PassThrough' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
1460:29-40 Can't import the named export 'PassThrough' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
496:11-22 Can't import the named export 'PassThrough' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
ERROR in ./node_modules/node-fetch/lib/index.mjs
497:11-22 Can't import the named export 'PassThrough' from non EcmaScript module (only default export is available)
@ ./node_modules/node-fetch/lib/index.mjs
@ ./apps/server/src/test.js
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
chris-feist commentedon Jul 23, 2018
@peterbakonyi05 This resolved it for me apollographql/react-apollo#1737 (comment)
TimothyGu commentedon Jul 25, 2018
I used
importwith fields for Node.js modules because they are now supported by Node.js after nodejs/node#20403 (v10.2.0).Would using only default import help with the problem? I.e.,
import http from 'http'; const { STATUS_CODES } = http;? This is compatible with Node.js as well.peterbakonyi05 commentedon Jul 26, 2018
Thanks @f3ist, I didn't manage to find that in the official docs. In case anyone needs more info why the below solves the problem: webpack/webpack#6357
tleef commentedon Aug 19, 2018
For what it's worth, you can also work around this issue more precisely by setting an
aliasin theresolvesection of your webpack configThis is similar to the original solution of using
mainFields: ["main", "module"]but only affectsnode-fetchUpdate to fix issues with building
yvele commentedon Oct 24, 2018
The
resolve.aliasworkaround is better and more precise thanjavascript/autoworkaround as stated here apollographql/react-apollo#1737 (comment)bitinn commentedon Nov 5, 2018
2.2.1 release should address this issue, if not please let me know.
Fixed `node-fetch` resolving for Webpack node-fetch/node-fetch#493