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

ts-node-dev fails with "Must use import to load ES Module" but tsc works fine #351

Open
mahdiahmadi73 opened this issue Nov 23, 2024 · 0 comments

Comments

@mahdiahmadi73
Copy link

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.

Project Setup:
File Structure:

/server
├── package.json
├── tsconfig.json
├── src
└── index.ts

package.json:

{
  "name": "server",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "dev": "ts-node-dev --esm src/index.ts",
    "build": "tsc"
  },
  "dependencies": {
    "express": "^4.21.1"
  },
  "devDependencies": {
    "typescript": "^5.6.3",
    "ts-node": "^10.9.1",
    "ts-node-dev": "^2.0.0",
    "@types/node": "^22.9.1",
    "@types/express": "^4.17.17"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "Node",
    "target": "ES6",
    "strict": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "outDir": "dist",
    "rootDir": "src"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

src/index.ts:

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:

  1. nodemon watches for changes in the files and restarts the process automatically.
  2. 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"
image

Tried replacing ts-node-dev with ts-node:

ts-node --esm src/index.ts
Still got this error :
image

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant