Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.mjs fails webpack build #493

Closed
peterbakonyi05 opened this issue Jul 23, 2018 · 6 comments
Closed

.mjs fails webpack build #493

peterbakonyi05 opened this issue Jul 23, 2018 · 6 comments

Comments

@peterbakonyi05
Copy link

peterbakonyi05 commented Jul 23, 2018

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

@chris-feist
Copy link

@peterbakonyi05 This resolved it for me apollographql/react-apollo#1737 (comment)

@TimothyGu
Copy link
Collaborator

TimothyGu commented Jul 25, 2018

I used import with 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
Copy link
Author

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

{
  type: 'javascript/auto',
  test: /\.mjs$/,
  use: []
}

@tleef
Copy link

tleef commented Aug 19, 2018

For what it's worth, you can also work around this issue more precisely by setting an alias in the resolve section of your webpack config

{
  ...
  resolve: {
    alias: {
      'node-fetch$': "node-fetch/lib/index.js"
    }
  }
  ...
}

This is similar to the original solution of using mainFields: ["main", "module"] but only affects node-fetch

@yvele
Copy link

yvele commented Oct 24, 2018

The resolve.alias workaround is better and more precise than javascript/auto workaround as stated here apollographql/react-apollo#1737 (comment)

Just remember Webpack's defaults are the way they are for a reason. .mjs is limited because it's not fully baked and it's support story isn't finished yet. If you need javascript/auto you should really be using .js.

@bitinn
Copy link
Collaborator

bitinn commented Nov 5, 2018

2.2.1 release should address this issue, if not please let me know.

@bitinn bitinn closed this as completed Nov 5, 2018
Jaid added a commit to Jaid/jaidbot that referenced this issue Jun 16, 2019
Signed-off-by: Jaid <jaid.jsx@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants