wclr / ts-node-dev

Compiles your TS app and restarts when files are modified.
MIT License
3.42k stars 123 forks source link

Error when using ESM that works with ts-node #212

Open daveisfera opened 3 years ago

daveisfera commented 3 years ago

When I use import with ts-node, it works, but then when I run the same script with ts-node-dev, then I get this error (NOTE: I forced resolution of ts-node to 9.0 to see if it would fix it, but I got the same error):

ts-node-dev ver. 1.0.0-pre.63 (using ts-node ver. 9.0.0, typescript ver. 4.0.3)
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/dlj/projects/mocha_deasync/test.js
    at Module._extensions..js (internal/modules/cjs/loader.js:1093:13)
    at Object.nodeDevHook [as .js] (/Users/dlj/projects/mocha_deasync/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Module.require (internal/modules/cjs/loader.js:965:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/dlj/projects/mocha_deasync/node_modules/ts-node-dev/lib/wrap.js:104:1)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Module._compile (/Users/dlj/projects/mocha_deasync/node_modules/source-map-support/source-map-support.js:547:25)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
[ERROR] 20:56:46 Error: Must use import to load ES Module: /Users/dlj/projects/mocha_deasync/test.js
nettybun commented 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

nettybun commented 3 years ago

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

cspotcode commented 3 years ago

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.

ltbyce commented 3 years ago

Same issue

jimmywarting commented 2 years ago

need this so badly right now...

SrBrahma commented 2 years ago

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?

cspotcode commented 2 years ago

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 .

comingAlive commented 2 years ago

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/

jimmywarting commented 2 years ago

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

cspotcode commented 2 years ago

@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.

jimmywarting commented 2 years ago

@cspotcode i haven't looked into it or tested it...

jimmywarting commented 2 years ago

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

cspotcode commented 2 years ago

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.