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

Local variable named module in an ESM node_module breaks build #57649

Closed
1 task done
devongovett opened this issue Oct 28, 2023 · 2 comments
Closed
1 task done

Local variable named module in an ESM node_module breaks build #57649

devongovett opened this issue Oct 28, 2023 · 2 comments
Labels
bug Issue was opened via the bug report template. locked

Comments

@devongovett
Copy link

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/nextjs-module-repro-257rt8?file=%2Fpackages%2Ftest%2Ftest.mjs%3A5%2C1

To Reproduce

Run next build in the above sandbox.

Current vs. Expected behavior

Build fails. It should succeed.

Failed to compile.

./packages/test/index.mjs
export 'foo' (reexported as 'foo') was not found in './test.mjs' (module has no exports)

Import trace for requested module:
./packages/test/index.mjs
./app/page.js


> Build failed because of webpack errors

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64
Binaries:
  Node: 18.18.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.7.6
Relevant Packages:
  next: 14.0.0
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Additional context

As shown in the codesandbox, the problem is due to a file in node_modules that has a function that assigns to a local variable called module, with a property called exports.

function test() {
  var module = { exports: {} };
  return module.exports;
}

export const foo = 2;

This seems to cause Next.js to think the file has no exports anymore. If you rename module to anything else it works fine. My guess is faulty CommonJS detection. That should only apply to the global module, not when a local variable that happens to be named module is defined. If this is found in node_modules, it is hard to workaround. This pattern is often found in CommonJS libraries that were published with an ESM wrapper.

@devongovett devongovett added the bug Issue was opened via the bug report template. label Oct 28, 2023
@devongovett devongovett changed the title Local variable named module in a node_module breaks build Local variable named module in an ESM node_module breaks build Oct 28, 2023
@huozhi
Copy link
Member

huozhi commented Dec 21, 2023

Should be fixed by #58967 already, upgrading to canary will work. Same as #57962

@huozhi huozhi closed this as completed Dec 21, 2023
Copy link
Contributor

github-actions bot commented Jan 5, 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 Jan 5, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

No branches or pull requests

2 participants