Skip to content

ESM in .mjs files cause a dev mode runtime error #17806

@jaydenseric

Description

@jaydenseric

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:

#13076

Metadata

Metadata

Assignees

Labels

WebpackRelated to Webpack with Next.js.good first issueEasy to fix issues, good for newcomerslocked

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions