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 build with standalone output and custom directory outputs CommonJS code instead of ESM #62521

Open
zollf opened this issue Feb 26, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving)

Comments

@zollf
Copy link

zollf commented Feb 26, 2024

Link to the code that reproduces this issue

https://github.com/zollf/nextjs-standalone-esm-bug

To Reproduce

  1. Add "type": "module" in package.json for ESM
  2. Add output: 'standalone' in next config
  3. Rename or add a custom directory to build from
  4. Build using custom directory npm run build custom-dir
  5. Run standalone server node custom-dir/.next/standalone/custom-dir/server.js

Current vs. Expected behavior

When trying to run a standalone server it will result in a reference error:ReferenceError: require is not defined in ES module scope, you can use import instead. Upon inspection server.js is using CommonJS, not ESM code.

Looking at the build utils this potentially could be the cause:

const packageJsonPath = path.join(distDir, '../package.json')
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'))
moduleType = packageJson.type === 'module'

Our package.json location is one more directory away.

The expected outcome is for standalone output to be ESM code with a custom directory

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:31 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8112
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 14.1.0
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: standalone

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

Module resolution (CJS / ESM, module resolving)

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

next build (local)

Additional context

No response

@zollf zollf added the bug Issue was opened via the bug report template. label Feb 26, 2024
@github-actions github-actions bot added the Module Resolution Module resolution (CJS / ESM, module resolving) label Feb 26, 2024
@DavieReid
Copy link

Have you found a workaround for this issue? I am using a monorepo (turbo with pnpm workspaces) and I have the same problem.

@zollf
Copy link
Author

zollf commented Aug 28, 2024

Have you found a workaround for this issue? I am using a monorepo (turbo with pnpm workspaces) and I have the same problem.

@DavieReid We've just placed this package.json

{
  "type": "module"
}

in all our project folders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving)
Projects
None yet
Development

No branches or pull requests

2 participants