denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
96.76k stars 5.34k forks source link

BYONM: Dynamically importing CommonJS file from an npm package errors #21149

Open bartlomieju opened 11 months ago

bartlomieju commented 11 months ago
$ deno run -A npm:create-next-app@latest next-app
$ cd next-app
$ deno run -A --unstable-byonm npm:next dev
error: Uncaught (in promise) ReferenceError: module is not defined
module.exports = nextConfig
^
    at file:///Users/ib/dev/next-app/next.config.js:6:1

Chaging module.exports = to export default fixes the problem.

Cedar-81 commented 11 months ago

is this a problem that needs to be fixed on Denos end? (Still new to open source, just trying to understand how it could be fixed)

dsherret commented 11 months ago

I disagree that this is a bug. The file is not located in an npm package. It's located at file:///Users/ib/dev/next-app/next.config.js, which means it is ESM. This can be fixed in next.config.js by changing:

module.exports = nextConfig;

To...

export default nextConfig;

...as Next.js loads this file via a dynamic import. We should update the error message to explain that module.exports = needs to be converted to ESM.