question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ESM in .mjs files cause a dev mode runtime error

See original GitHub issue

Bug 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

Screen Shot 2020-10-12 at 1 44 24 pm

Here is the call stack:

Screen Shot 2020-10-12 at 1 45 02 pm

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create a basic Next.js project.

  2. Create a config.mjs file containing:

    export const A = 'a';
    
  3. In pages/index.js:

    import { A } from '../config.mjs';
    
    export default function IndexPage() {
      return A;
    }
    
  4. 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:

https://github.com/vercel/next.js/discussions/13076

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:30
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
JacobLeycommented, Mar 22, 2021

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

Just because Next.js has the ability to hack together webpack and Babel configs to make non standard things work due to a build step, doesn’t mean it should. It’s better for the ecosystem if all our tools work to common standards

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

Next.js should only support ESM in .js files if the project package.json contains “type”: “module”

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found