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

Cannot import from .mjs file with locally scoped module.exports = code. #57962

Closed
1 task done
tmeasday opened this issue Nov 2, 2023 · 3 comments · Fixed by #58967
Closed
1 task done

Cannot import from .mjs file with locally scoped module.exports = code. #57962

tmeasday opened this issue Nov 2, 2023 · 3 comments · Fixed by #58967
Assignees
Labels
bug Issue was opened via the bug report template. locked Module Resolution Module resolution (CJS / ESM, module resolving)

Comments

@tmeasday
Copy link

tmeasday commented Nov 2, 2023

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/modern-currying-yz577d

In .mjs file:

// This is just the simplest way to repro, you can see realistic esbuild code below
(module) => {
  module.exports = {};
};

export const bar = "baz";

To Reproduce

  1. Open https://codesandbox.io/p/sandbox/modern-currying-yz577d above
  2. Notice the error: Attempted import error: 'bar' is not exported from './file.mjs' (imported as 'bar').
  3. Comment out unused module.exports = line
  4. Notice it works
  5. Add the line back, and rename the file to file.js (from file.mjs)
  6. Notice it works

Current vs. Expected behavior

I would expect that the compiler would ignore module.exports = {} where module is a local variable.

The above code is similar to code generated by esbuild:

var require_entities = __commonJS({
  '../../node_modules/entities/lib/maps/entities.json'(exports, module) {


  module.exports = // internal exports
});

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
Binaries:
  Node: 18.18.2
  npm: 9.8.1
  Yarn: 1.22.18
  pnpm: 8.6.2
Relevant Packages:
  next: 14.0.2-canary.8
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 4.9.4
Next.js Config:
  output: N/A

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

App Router

Additional context

I reproduced locally on canary but the code sandbox repro template is fixed to node 16 so I wasn't sure how to upgrade. Still the behaviour seems the same.

The code above is included in a file when dependencies are prebundled, e.g. with a tool like tsup. This symptom happens for the Storybook packages, which use tsup and generate .mjs files.

I notice the error is the same as #47945 and #48091. There isn't really enough context on those issues to say if it's the same problem.

@lukasIO
Copy link

lukasIO commented Nov 17, 2023

looks like there's a potential fix here

@LuisGilGB
Copy link

Reproduction of a possibly related bug: #57784 (comment)

@huozhi huozhi self-assigned this Nov 30, 2023
shilman pushed a commit to storybookjs/storybook that referenced this issue Dec 2, 2023
To workaround vercel/next.js#57962

cc @ndelangen. Not sure we want to merge this but it gets us through the demo for now.
Copy link
Contributor

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 locked as resolved and limited conversation to collaborators Dec 16, 2023
@huozhi huozhi added the Module Resolution Module resolution (CJS / ESM, module resolving) label Jan 2, 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 Module Resolution Module resolution (CJS / ESM, module resolving)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants