You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’m facing an issue when using ts-node-dev to run my TypeScript project. Whenever I execute the following command (By npm run dev script):
ts-node-dev src/index.ts
I get this error:
Must use import to load ES Module: /path/to/src/index.ts
However, if I compile the code with tsc and run the compiled JavaScript file with Node.js, everything works perfectly:
tsc src/index.ts
node dist/index.js
I have tried multiple solutions but nothing seems to fix the issue.
import express from "express";
const app = express();
app.get("/", (req, res) => {
res.send("Hello, world!");
});
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});
The only solution I found is this #314
To fix the issue, I replaced the ts-node-dev command with the following:
nodemon --exec node --loader ts-node/esm src/index.ts
This ensures:
nodemon watches for changes in the files and restarts the process automatically.
ts-node/esm handles TypeScript compilation and ES module support.
What I Tried Before:
Verified my Node.js version (v20) and ensured it supports ES modules.
Ensured ts-node and ts-node-dev are updated to their latest versions.
Tried adding and removing "type": "module" in package.json.
Added the --esm flag to the dev script:
"dev": "ts-node-dev --esm src/index.ts"
Tried replacing ts-node-dev with ts-node:
ts-node --esm src/index.ts
Still got this error :
Compiled with tsc and ran the output with node, which worked fine.
tsc /src/index.ts
node dist/index.js
OS version (is it docker or host?), ts-node-dev version
Mac OS 15.0.1( host) . ts-node-dev version : 2.0.0
Did you try to run with ts-node?
No Did you try to run with --files option enabled?
No Did you try to run with --debug option enabled?
No Do you have a repro example (git repo) with simple steps to reproduce your problem?
No
The text was updated successfully, but these errors were encountered:
I’m facing an issue when using ts-node-dev to run my TypeScript project. Whenever I execute the following command (By npm run dev script):
ts-node-dev src/index.ts
I get this error:
Must use import to load ES Module: /path/to/src/index.ts
However, if I compile the code with tsc and run the compiled JavaScript file with Node.js, everything works perfectly:
I have tried multiple solutions but nothing seems to fix the issue.
Project Setup:
File Structure:
/server
├── package.json
├── tsconfig.json
├── src
└── index.ts
package.json:
tsconfig.json:
src/index.ts:
The only solution I found is this
#314
To fix the issue, I replaced the ts-node-dev command with the following:
nodemon --exec node --loader ts-node/esm src/index.ts
This ensures:
What I Tried Before:
Verified my Node.js version (v20) and ensured it supports ES modules.
Ensured ts-node and ts-node-dev are updated to their latest versions.
Tried adding and removing "type": "module" in package.json.
Added the --esm flag to the dev script:
"dev": "ts-node-dev --esm src/index.ts"
Tried replacing ts-node-dev with ts-node:
ts-node --esm src/index.ts
Still got this error :
Compiled with tsc and ran the output with node, which worked fine.
OS version (is it docker or host?), ts-node-dev version
Mac OS 15.0.1( host) . ts-node-dev version : 2.0.0
Did you try to run with ts-node?
No
Did you try to run with
--files
option enabled?No
Did you try to run with
--debug
option enabled?No
Do you have a repro example (git repo) with simple steps to reproduce your problem?
No
The text was updated successfully, but these errors were encountered: