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

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

Closed
Tracked by #31
jaydenseric opened this issue Oct 12, 2020 · 12 comments
Closed
Tracked by #31

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

jaydenseric opened this issue Oct 12, 2020 · 12 comments
Assignees
Labels
good first issue Easy to fix issues, good for newcomers locked Webpack Related to Webpack with Next.js.

Comments

@jaydenseric
Copy link
Contributor

jaydenseric commented Oct 12, 2020

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

@timneutkens timneutkens added good first issue Easy to fix issues, good for newcomers kind: bug labels Oct 12, 2020
@meglio
Copy link

meglio commented Mar 8, 2021

In the meantime, is there any workaround to use .mjs files in the dev mode? I'm trying to introduce Next.js to an existing project, but this prevents me from doing so. Please advise.

@jerrygreen
Copy link
Contributor

jerrygreen commented Mar 8, 2021

Am I the single one here who doesn’t understand why choose mjs over js? Imports? Already supported in NextJS without having to use mjs. WASM modules? I already experimented with this and can assure you it’s also working already. Is it top level await or import.meta.url or what?
P.S. no sarcasm btw, just wanna know

@jaydenseric
Copy link
Contributor Author

@jerrygreen Next.js must support standard Node.js file extensions such as .mjs and .cjs without errors or bugs. If you are really interested in the design decisions behind the Node.js API for modules, read through the issues, PRs, and meeting notes of the Node.js Modules Team. Honestly, it's not worth the time for someone coming onto the scene now as there are many comments to read through; just follow the final rules as documented for the Node.js API.

Going further, Next.js should only support ESM in .js files if the project package.json contains "type": "module". If Next.js continues to refuse to adhere to Node.js standards regarding module formats I'll move to something else, or even create it myself if need be.

Already supported in NextJS without having to use mjs.

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; e.g. you want to be able to use in a Next.js project the same ESLint config you use for linting imports in your published Node.js packages.

@jerrygreen
Copy link
Contributor

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

Oh, now I can finally see the reasoning! Yeah, I actually agree it should be supported, and less webpack hacks used: it's better to be handled by node since it does support all that too. Well, good thing Timer added this to the backlog milestone so hopefully it will be fixed sometime soon

@JacobLey
Copy link
Contributor

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 #17806 (comment) 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

joostdecock added a commit to freesewing/freesewing that referenced this issue Jul 31, 2021
This comes from @joostdecock 's fork

commit 4d854f6e9817f9fa9c83fa0f0566dd8cb0aba876
Merge: 66ef672 8ca954d
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 31 15:18:40 2021 +0200

    Merge branch 'develop' of https://github.com/joostdecock/freesewing into joostdecock-develop

commit 8ca954d
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 31 15:05:07 2021 +0200

    chore: Final lingering changes prior to import

commit 045bd9b
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 31 15:04:48 2021 +0200

    chore: Flat import of markdown repo files

commit 897bb75
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 31 15:04:15 2021 +0200

    chore: Removed markdown submodule

commit 52c7158
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 31 15:03:17 2021 +0200

    chore: Removed markdown submodule

commit 1e722d2
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 18:07:13 2021 +0200

    fix: Handle missing language

commit 61ebf0c
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 18:07:01 2021 +0200

    fix: Handle missing t method

commit a264ab3
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 18:03:26 2021 +0200

    fix: pass language to mdx loadFile

commit e33014a
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 18:03:03 2021 +0200

    fix: Allow collapsing active page

commit 2f4fc93
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 17:39:23 2021 +0200

    fix: margin-auto for images

commit 0aa139f
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 17:11:05 2021 +0200

    fix: Handle missing author

commit 88eb14a
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 17:02:31 2021 +0200

    feat(i18n): Added showcase

commit 64e951f
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 17:02:08 2021 +0200

    feat: Added showcase posts to tree

commit 864ea03
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 28 16:16:58 2021 +0200

    chore: Updated config

commit 1e0136f
Author: joostdecock <joost@joost.at>
Date:   Tue Jul 27 17:57:50 2021 +0200

    fix: Better mobile search

commit e2348b7
Author: joostdecock <joost@joost.at>
Date:   Tue Jul 27 17:47:29 2021 +0200

    fix: Better mobile menu sizing

commit 4dafc17
Author: joostdecock <joost@joost.at>
Date:   Mon Jul 26 19:04:01 2021 +0200

    wip: Fixes for deploy

commit 2e9d4fc
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 25 18:39:32 2021 +0200

    chore: Ported latest changes to dev

commit a90d417
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 25 18:21:27 2021 +0200

    chore: Reworking i18n strings

commit f3e8dfb
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 25 18:21:15 2021 +0200

    feat: showcase posts

commit c803cc2
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 24 16:51:45 2021 +0200

    chore: Work on navbar

commit 36a35a7
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 24 14:56:56 2021 +0200

    chore: Better mdx code sharing

commit 6b7b3d2
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 24 14:23:29 2021 +0200

    chore: Better strapi code sharing

commit 8208d93
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 24 13:53:01 2021 +0200

    chore: Updated to strapi changes

commit 7050629
Author: joostdecock <joost@joost.at>
Date:   Wed Jul 21 19:13:05 2021 +0200

    wip: MDX support across sites

commit b5dc4b5
Author: joostdecock <joost@joost.at>
Date:   Tue Jul 20 19:46:37 2021 +0200

    feat: Allow collapsing of aside menu

commit c0869b0
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 18 20:24:36 2021 +0200

    wip: Better navigation

commit 8562da2
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 18 20:05:32 2021 +0200

    wip: Re-working tree navigation

commit 260cadd
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 18 18:06:28 2021 +0200

    wip: Re-working tree navigation

commit 1651adf
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 18 16:03:04 2021 +0200

    wip: Work on tree navigation

commit c9121c6
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 18 10:56:08 2021 +0200

    wip: Working on next sites

commit 37b34e1
Author: joostdecock <joost@joost.at>
Date:   Sun Jul 18 10:43:23 2021 +0200

    wip: Still working on getting next to play nice

commit dd23727
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 17 19:53:52 2021 +0200

    wip: Various build changes

    Disabled the tailwind JIT mode as it was giving problems
    Moved to next-standard way of using import aliases
    Added a prebuild step to load mdx/strapi/tree content

    This is a WIP

commit d02630f
Author: joostdecock <joost@joost.at>
Date:   Sat Jul 17 15:36:40 2021 +0200

    breaking(i18n): Switched to named exports, renamed files

    I guess this makes it official: Merging in this fork will require a
    major version change because there are a bunch of breaking changes here.

     - We now use named exports in i18n
     - We export languages individually to facilitate tree shaking
     - We pre-build all strings, so we don't need to ship with dependencies

commit a00eeaa
Author: joostdecock <joost@joost.at>
Date:   Fri Jul 16 17:30:03 2021 +0200

    feat: Support for MDX in blog posts

commit afaa1fc
Author: joostdecock <joost@joost.at>
Date:   Fri Jul 16 12:22:20 2021 +0200

    feat: Initial setup of freesewing.org

commit b9b7f43
Author: joostdecock <joost@joost.at>
Date:   Thu Jul 15 19:35:58 2021 +0200

    chore: Prevent pruning of theme colors

commit c642ba2
Author: joostdecock <joost@joost.at>
Date:   Thu Jul 15 18:59:42 2021 +0200

    chore: Style tweaks

commit d5defa5
Author: joostdecock <joost@joost.at>
Date:   Thu Jul 15 17:27:08 2021 +0200

    chore: Tweaks to component styles

commit 654c4d3
Author: joostdecock <joost@joost.at>
Date:   Tue Jul 13 18:38:12 2021 +0200

    chore: Tweaks to mobile view

commit 49d3516
Author: joostdecock <joost@joost.at>
Date:   Mon Jul 12 16:55:13 2021 +0200

    chore: Updated dependencies

commit 9bcbdcb
Author: joostdecock <joost@joost.at>
Date:   Mon Jul 12 16:45:29 2021 +0200

    fix: Submodule settings

commit 9072805
Author: joostdecock <joost@joost.at>
Date:   Mon Jul 12 16:44:04 2021 +0200

    chore: Initial build

commit 4f248ed
Author: joostdecock <joost@joost.at>
Date:   Mon Jul 12 15:58:13 2021 +0200

    fix(pattern-info): Order of patterns

commit 65a7d6b
Author: joostdecock <joost@joost.at>
Date:   Mon Jul 12 15:26:59 2021 +0200

    chore: Switched submodule to ssh

commit f776fae
Author: joostdecock <joost@joost.at>
Date:   Mon Jul 12 15:26:47 2021 +0200

    wip: 2022

commit 052dbc6
Author: Joost De Cock <joost@joost.at>
Date:   Sat Jul 10 18:03:22 2021 +0200

    chore: Theme tweaks

commit 2e43b0d
Author: Joost De Cock <joost@joost.at>
Date:   Sat Jul 10 17:46:44 2021 +0200

    feat(nextjs): Added footer

commit 08a99e4
Author: Joost De Cock <joost@joost.at>
Date:   Sat Jul 10 15:56:16 2021 +0200

    feat(nextjs): Added pattern styling

commit b3dc673
Author: Joost De Cock <joost@joost.at>
Date:   Sat Jul 10 15:04:13 2021 +0200

    chore: Search improvements

commit 74f3667
Author: Joost De Cock <joost@joost.at>
Date:   Sat Jul 10 15:03:22 2021 +0200

    Fix: Workaround for NextJS ESM bug

    See vercel/next.js#17806

commit 06fc77f
Author: Joost De Cock <joost@joost.at>
Date:   Fri Jul 9 17:48:25 2021 +0200

    feat(nextjs): Added PrevNext component

commit 6f694da
Author: Joost De Cock <joost@joost.at>
Date:   Fri Jul 9 15:49:47 2021 +0200

    feat(nextjs): Let tree component handle ReadMore

commit 2347c19
Author: Joost De Cock <joost@joost.at>
Date:   Fri Jul 9 09:32:49 2021 +0200

    feat(nextjs): Close search button on mobile

commit ca72776
Author: Joost De Cock <joost@joost.at>
Date:   Fri Jul 9 08:50:21 2021 +0200

    feat(nextjs): Implemented keyboard-driven search

commit 5bf952d
Author: Joost De Cock <joost@joost.at>
Date:   Tue Jul 6 17:10:39 2021 +0200

    chore: Removed lingering console.log

commit 70397a3
Author: Joost De Cock <joost@joost.at>
Date:   Tue Jul 6 15:50:19 2021 +0200

    wip(nextjs): Save prior to moving to a different machine

commit 5775d8e
Author: Joost De Cock <joost@joost.at>
Date:   Tue Jul 6 09:23:30 2021 +0200

    wip(nextjs): Some further style tweaks

commit a30ff5c
Author: Joost De Cock <joost@joost.at>
Date:   Mon Jul 5 14:48:28 2021 +0200

    feat(nextjs): Allow toggling top-level navigation

commit 469d396
Author: Joost De Cock <joost@joost.at>
Date:   Mon Jul 5 14:47:45 2021 +0200

    fix(nextjs): Handle unknown languages in prism

commit e1f8d08
Author: Joost De Cock <joost@joost.at>
Date:   Mon Jul 5 14:47:15 2021 +0200

    feat(nextjs): Added down icon

commit c610c56
Author: Joost De Cock <joost@joost.at>
Date:   Mon Jul 5 10:00:17 2021 +0200

    feat(nextjs): Added syntax highlighting

commit 9d5cb83
Author: Joost De Cock <joost@joost.at>
Date:   Sun Jul 4 18:30:48 2021 +0200

    wip(nextjs): Started work on freesewing.dev on NextJS
@graup
Copy link

graup commented Sep 6, 2021

The mentioned workaround also seems to have solved this issue for me.

Here's my config:

  webpack: (config) => {
    config.module.rules.push({
      test: /\.m?js$/,
      type: 'javascript/auto',
      resolve: {
        fullySpecified: false,
      },
    });
    return config;
  },

I'm using .ts files with { type: module } in package.json.

@styfle styfle modified the milestones: 11.1.x, 12.0.4 Nov 5, 2021
@timneutkens timneutkens added the Webpack Related to Webpack with Next.js. label Nov 16, 2021
@timneutkens timneutkens removed this from the 12.0.5 milestone Nov 17, 2021
@gpoole
Copy link

gpoole commented Jan 13, 2022

Thanks @graup, worked around the issue for me on Next 11.1.3.

@hutber
Copy link

hutber commented Dec 31, 2023

The mentioned workaround also seems to have solved this issue for me.

Here's my config:

  webpack: (config) => {
    config.module.rules.push({
      test: /\.m?js$/,
      type: 'javascript/auto',
      resolve: {
        fullySpecified: false,
      },
    });
    return config;
  },

I'm using .ts files with { type: module } in package.json.

Sadly with next 14 its moved to rust compiler so if you want to take advantage of the new speeds, we'll need to find another work around.

@joulev
Copy link
Contributor

joulev commented Jan 5, 2024

#58967 should already fix this I think, the workaround should no longer be required.

@samcx
Copy link
Member

samcx commented Feb 21, 2024

Hi everyone!

This seems to already be fixed on the latest, so I will be closing this one out!

If you are still coming across this issue, please feel free to send us a new bug report.

@samcx samcx closed this as completed Feb 21, 2024
Copy link
Contributor

github-actions bot commented Mar 7, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Mar 7, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers locked Webpack Related to Webpack with Next.js.