ReasonJun

Nextjs : Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (ts-node) 본문

Frontend/Next.js

Nextjs : Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (ts-node)

ReasonJun 2023. 11. 6. 13:50
728x90

 

What happened?

In the case of ts-node, ts was compiled using the commonJS method. Therefore, an error occurs when you try to run a ts file with ts-node in the app or src subfolder of nextjs, which is compiled with esNext.

 

 

Error :

https://github.com/TypeStrong/ts-node/issues/935

 

`ts-node` fails when ES Modules are in the dependency graph in Node.js 13+ · Issue #935 · TypeStrong/ts-node

I basically detailed the issue in this comment: #155 (comment) It's a chicken-and-egg-like problem: If we use module: 'commonjs', then if any TS files import ES Modules (indirectly in their depende...

github.com

 

 

Solution : 

https://www.npmjs.com/package/ts-node?activeTab=readme#commonjs

 

ts-node

TypeScript execution environment and REPL for node.js, with source map support. Latest version: 10.9.1, last published: a year ago. Start using ts-node in your project by running `npm i ts-node`. There are 9079 other projects in the npm registry using ts-n

www.npmjs.com

https://stackoverflow.com/questions/69870137/running-a-single-ts-file-in-a-next-js-project-with-ts-node-for-testing

 

Running a single .ts file in a Next.js project with ts-node for testing

I'm trying to run a single ES module .ts file in a Next.js project with default config for quick debugging: npx ts-node lib/my_module.ts which gives me this error: Warning: To load an ES module, s...

stackoverflow.com

 

728x90
Comments