ESM build with standalone output and custom directory outputs CommonJS code instead of ESM #62521
Labels
bug
Issue was opened via the bug report template.
Module Resolution
Module resolution (CJS / ESM, module resolving)
Link to the code that reproduces this issue
https://github.com/zollf/nextjs-standalone-esm-bug
To Reproduce
"type": "module"
inpackage.json
for ESMoutput: 'standalone'
in next confignpm run build custom-dir
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 inspectionserver.js
is using CommonJS, not ESM code.Looking at the build utils this potentially could be the cause:
next.js/packages/next/src/build/utils.ts
Lines 1948 to 1950 in bef716a
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
The text was updated successfully, but these errors were encountered: