Open daveisfera opened 3 years ago
I think I'm running into the same thing when trying to load the ts-node/esm
loader
> ts-node-dev -r ts-node/esm/transpile-only src/index.ts
[INFO] 13:56:13 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 4.0.5)
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/today/_/work/beadloom/packages/server/node_modules/ts-node/esm/transpile-only.mjs
at Module.load (internal/modules/cjs/loader.js:1038:11)
at Function.Module._load (internal/modules/cjs/loader.js:929:14)
at Module.require (internal/modules/cjs/loader.js:1080:19)
at Module._preloadModules (internal/modules/cjs/loader.js:1321:12)
at loadPreloadModules (internal/bootstrap/pre_execution.js:439:5)
at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:71:3)
at internal/main/run_main_module.js:7:1
Note that ts-node works fine:
> npm run start
> etch-server@0.0.0 start /home/today/_/work/beadloom/packages/server
> node --loader ts-node/esm/transpile-only src/index.ts
(node:55076) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Koa server listening on http://localhost:3000/
...etc
I imagine that -r
is using require
under the hood which isn't compatible with ESM
Which in my case is related to https://github.com/whitecolor/ts-node-dev/issues/65#issuecomment-690856333 and https://github.com/TypeStrong/ts-node/issues/1007
As best I understand, ts-node-dev
doesn't install ts-node
into the child-process. It installs its own hooks into the child process which delegate compilation to the parent process.
Unfortunately, node's ESM loaders (still an experimental, unstable API) require a totally different kind of hooking. So if ts-node-dev hasn't implemented this new, unstable form of hooking, then it won't be able to compile files which are loaded as native ESM.
Same issue
need this so badly right now...
After spending my entire day on this ESM matter, I am now using nodemon with the following nodemon.json in project root:
{
"restartable": "rs",
"ignore": [
".git",
"node_modules/**/node_modules",
"lib/**/*"
],
"execMap": {
"ts": "node --loader ts-node/esm"
},
"ext": "ts,js,json"
}
While it works and it restarts my project on file change, isn't as fast as ts-node-dev.
Does someone has a better (= faster) alternative?
Try enabling ts-node's "swc" integration. The docs have the info you need.
On Sat, Nov 20, 2021, 2:23 PM Henrique Bruno @.***> wrote:
After spending my entire day on this ESM matter, I am now using nodemon with the following nodemon.json in project root:
{ "restartable": "rs", "ignore": [ ".git", "node_modules//node_modules", "lib//*" ], "execMap": { "ts": "node --loader ts-node/esm" }, "ext": "ts,js,json" }
While it works and it restarts my project on file change, isn't as fast as ts-node-dev.
Does someone has a better (= faster) alternative?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wclr/ts-node-dev/issues/212#issuecomment-974699400, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC35OBLMM23WZCDSWB3FXDUM7YURANCNFSM4SFOE7LQ .
Try enabling ts-node's "swc" integration. The docs have the info you need. … On Sat, Nov 20, 2021, 2:23 PM Henrique Bruno @.*> wrote: After spending my entire day on this ESM matter, I am now using nodemon with the following nodemon.json in project root: { "restartable": "rs", "ignore": [ ".git", "node_modules//node_modules", "lib/*/" ], "execMap": { "ts": "node --loader ts-node/esm" }, "ext": "ts,js,json" } While it works and it restarts my project on file change, isn't as fast as ts-node-dev. Does someone has a better (= faster) alternative? — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#212 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC35OBLMM23WZCDSWB3FXDUM7YURANCNFSM4SFOE7LQ .
Thank you. An example, if someone needs it: https://github.com/comingAlive/typescript-nodemon-top-level-await-example/
from what i read over at https://github.com/fgnass/node-dev/pull/234 the original node-dev package now supports typescript loaders and esm
perhaps this ts-node-dev is no longer needed
@jimmywarting Interesting, do you know if it supports ts-node's esm loader? The linked PR shows that it supports ts-node/register
, but node API limitations prevent us from installing our ESM hooks via that entrypoint. I'd be interested to hear if they also support --loader ts-node/esm
or some equivalent.
@cspotcode i haven't looked into it or tested it...
based on how NodeJS docs discourage the use of require.extensions
(b/c it's deprecated) and using custom loaders it seems as if they want you to always compile to javascript a head of time
So basically you should turn on typescripts own watcher and compile to a dist folder. and when any file in that dist folder change then you should reload the node process
i have to agree that this seems like the best approach using a experimental-module-loaders isn't as good... have to agree with https://stackoverflow.com/a/8947021/1008999 that having multiple languages supported would be a nightmare
Every once in a while (past issues) the require.extensions
deprecation question comes up, and I think my comments here are still useful:
https://github.com/TypeStrong/ts-node/issues/1302#issuecomment-831578018
Deno natively supports TS by transforming TS to JS in-process. For them, supporting multiple languages is not a priority. I suspect that is also true for most TypeScript projects: the goal is to support TypeScript, not anything else.
If anyone is interesting in following along with discussion by the loaders team: https://github.com/nodejs/loaders They do want to enable transformation of code in-process, and they're working on loader composition so that multiple loaders can be used together.
Everyone should choose how they want to do things in their own projects, and this information should be helpful.
When I use
import
withts-node
, it works, but then when I run the same script withts-node-dev
, then I get this error (NOTE: I forced resolution ofts-node
to 9.0 to see if it would fix it, but I got the same error):