ESM in .mjs files cause a dev mode runtime error
See original GitHub issueBug report
Describe the bug
Importing a project ESM .mjs
file (the Node.js standard file extension for ESM) in a page file causes a runtime error, but only in dev mode:
Unhandled Runtime Error ReferenceError: module is not defined
Here is the call stack:
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
-
Create a basic Next.js project.
-
Create a
config.mjs
file containing:export const A = 'a';
-
In
pages/index.js
:import { A } from '../config.mjs'; export default function IndexPage() { return A; }
-
In Terminal, run:
npm run dev
You should then see no error in Terminal, but on the client you will see the Next.js error overlay with the error described earlier.
Expected behavior
You should be able to use ESM .mjs
files in your Next.js project without runtime errors in dev mode.
System information
- Version of Next.js: v9.5.5
Additional context
Other Next.js users have also encountered this issue:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:30
- Comments:8 (5 by maintainers)
Top Results From Across the Web
How to use MJS files in Node.js? - DEV Community
Learn how to import and export from MJS files in Node.js applications. ... node:internal/process/esm_loader: internalBinding('errors').
Read more >Using ECMAScript modules (ESM) with Node.js
When it comes to ES modules, it's well known that they're static — i.e, you can only “go to” them at compilation time,...
Read more >Node Modules at War: Why CommonJS and ES ... - Code Red
In ESM scripts, import and export are part of the language; like CJS, they have two different syntaxes for named exports and the...
Read more >What does it take to support Node.js ESM? – The Guild
I have worked on all The Guild's libraries and graphql-js to support ESM. Here is how you can do it too.
Read more >Gatsby Changelog | 5.3.0
ES Modules (ESM) in Gatsby files; Improved error messages ... In this release, we've included support for ESM in gatsby-node.mjs and gatsby-config.mjs ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have recently run into this issue as well, using
.js
with"type":"module"
In my use case, NextJS is just a part of a larger monorepo, all written with ESM. I definitely support the comment
In most cases it is great because it allows features like
import
/export
and top-level-await, all without needing a special transpiler like Babel or Typescript (I still use Typescript, but with ES module/target, so no transpiling). But NextJS has trouble loading some of those utility files.That twitter link https://github.com/vercel/next.js/issues/17806#issuecomment-707321843 actually appears to have fixed it for me, but it would be great to see this as a first-class feature and “just work” in NextJS without extra setup.
I will say I disagree with
While that is most “correct”, that would be breaking for most usage, which is writing ESM and expect it to automagically transpile into CommonJS.
If someone is deploying NextJS on its own (e.g. via Vercel, no custom server, no monorepo…) then Babels unambiguous sourceType is plenty acceptable to me
https://twitter.com/ScriptedAlchemy/status/1315424669026058240?s=20