ts-node-dev: [ERROR] Must use import to load ES module
Issue description

Context
Totally new Node project with typescript, @types/node, and ts-node-dev packages installed.
I only get this error when I set type: "module" in package.json.
ts-node-dev: ^2.0.0
OS version (is it docker or host?), ts-node-dev version
Did you try to run with ts-node?
Did you try to run with --files option enabled?
Did you try to run with --debug option enabled?
Do you have a repro example (git repo) with simple steps to reproduce your problem?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 75
- Comments: 43
Links to this issue
Commits related to this issue
- fix: docs:export Replace ts-node-dev with nodemon + ts-node: https://github.com/wclr/ts-node-dev/issues/314#issuecomment-1243067589 — committed to supabase/postgres-meta by soedirgo a year ago
- fix: docs:export Replace ts-node-dev with nodemon + ts-node: https://github.com/wclr/ts-node-dev/issues/314#issuecomment-1243067589 — committed to supabase/postgres-meta by soedirgo a year ago
- build: use nodemon/ts-node-dev in dev and prod I recognize this may not be optimal for performance, but it's simpler to set up. I might revise this approach in the future. Found the command here: htt... — committed to louis-marceron/BACK-AWI by louis-marceron 8 months ago
Update
Besides it working with Babel, I achieved to make it working with
nodemonandts-node.Install nodemon and ts-node and of course typescript, set
type: "module"inpackage.json, and settsconfig.jsonto:And then:
nodemon --exec node --loader ts-node/esm src/main.tsI’m having the same problem
ts-node-dev version: 2.0.0 typescript version: 4.7.2
You can try this config, I tested it with most ES2022 features (including Top Level Await) and it works.
if anyone want an alternative, i use tsx for about sometime and i love it, simple to use and maintained.
@angelhdzmultimedia,
We should somehow tell
ts-nodeto use the loader that supports ESM. This can be done in one of the following ways:1. Specify in
tsconfig.jsonthat thets-nodeuses ESM loaderIn this case, you don’t need to specify additional arguments in nodemon command:
2. Specify the
ts-nodecommand with ESM in nodemon:or
In this case, you don’t need to change the
tsconfig.jsonfile.3. Set
ts-nodewith ESM as the node loaderor
I prefer the first case (set
ts-node.esmto true in tsconfig), because it looks cleaner.I solved my problem by simply removing “type” : “module” from package.json
and still nothing?
I’m using tsx now. No issues so far.
But thanks for the update. I’m gonna try ts-node-dev again with module resolution set to nodenext.
Is the ts-node set to esm mandatory?
Bun for Windows is out guys! 🔥👀✌️🤣
Bun, tsx, unjs/jiti. Solid options now.
Closing this.
Sorry, but this is not a solution.
It is built on top of ts-node, and ts-node is a … please search on stackoverflow for solutions like
--esm, remove{ "type": "module" }… you will end up being recommended withtsxany way.I don’t know what you’re trying to achieve, but I got it working with:
npx nodemon -e ts,json --exec node --experimental-specifier-resolution=node --loader ts-node/esm main.tsWhere
main.tsis my main file in the root of my project.For me it is. Works fine.
My package.json:
{ "name": "general", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "dev": "ts-node-dev --respawn --transpile-only src/app.ts" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "ts-node-dev": "^2.0.0", "typescript": "^5.1.6" } }According to the typescript documentation the
moduleResolutionmust be:nodefor Node.js’ CommonJS implementationnodenextfor Node.js’ ECMAScript Module Support from TypeScript 4.7 onwards.So the
moduleResolutionmust benodenextfor ESM. I created the repository with minimum configuration for restarting the node app when the file changes. It usesnodemon, because thets-node-envdoesn’t seem to support ESM yet.package.json
tsconfig.json
i solved this problem removing “type”: “module” from my package.json and keeping just “module”: “CommonJS” on my tsconfig.json. it works fine.
Same problem here. I tried with
and it works BUT it’ll not restart automatically if files will change
Yeah, yeah, “solved” and “works fine” (always were the words of the ignorant) - see you again later in the day when you figure out it’ll blow on all these “EVIL”
ESM only packagesor any other valid reason forESM, e.g. publishing a package for others to use - then noESM?! 🙀Repo is looking stale, fair to say this isn’t getting fixed any time soon?
Seems that
ts-node-esmandts-node --esmare aliases of commands.ts-node/esmis the node loader.I don’t see any of them being deprecated at the moment. So you can use any of these methods that you like best.
@angelhdzmultimedia Wow,
tsxlooks even better. I’ll try it. Thanks.Unfortunately, I don’t quite understand your question. To make ts-node compatible with ESM, we should use the appropriate loader in one of the following ways, but
ts-node-dev(v2.0.0) still throws an error if ESM is used (Must use import to load ES Module).P.S.
tsxdoes not support type checking. We can, of course, run the TS compiler at firsttsc --noEmit && tsx watch src/index.ts, but in this case, if we violated some types during watch mode,tsxdoesn’t show any errors. This seems to be the main reason whytsxis faster than, for example,ts-node. IMO it’s inconvenient, because we don’t immediately know about mistakes in types.We are looking forward to ESM support in
ts-node-dev. In the meantime, we can usenodemon+ts-nodeif we need to check types in watch mode, ortsxif not.I wonder what issues await the brave on that path (with latest Node, etc.), but would you share with us - why it’s not
TSXanymore for you?Just using it a day now, but works great so far, even already with
Node21.7.1.And beside tons of features, it can also watch, same as
TS-Node-Dev:I’m lately using
node --watch ts-node/esm src/main.tsAnd in
tsconfig.json: { “ts-node”: { “experimentalSpecifierResolution”: “node”, “transpileOnly”: true, “esm”: true, } }And works… good… I dream of a day where there’s only 1 configuration and working with TypeScript + Node is a pleasure… Waiting for Bun Windows 👀
Thanks @angelhdzmultimedia and @ThallesP, works as great as you say - WoooF! 😻 😹💦
Thanks to your suggestions I got it working! There is one thing I didn’t get, I’m using TS, why do I need to import file adding the extension and this extension has be in
.js?not working on windows 😦
Got one-liner working with nodemon:
mark