wclr / ts-node-dev

Compiles your TS app and restarts when files are modified.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add `esm` to `ts-node` flags

ricardo-valero opened this issue · comments

I'm trying to run

ts-node-dev --experimental-specifier-resolution=node --esm --transpile-only src/index.ts

I'm getting this message

bad option: --esm

const tsNodeFlags = {

Just ran into the same issue, any known workaround?

commented

Just adding esm flag here won't work (it is not an ts-node API option which is used by this package), need to dig a bit deeper on how to make ESM work correctly.

Doesn't ts-node-dev -r esm work anyway?

Running

ts-node-dev -r esm src/index.ts

or

ts-node-dev --experimental-specifier-resolution=node --respawn -r esm src/index.ts

Returns

[INFO] 09:41:12 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.8.1, typescript ver. 4.7.3)
node:internal/modules/cjs/loader:942
  throw err;
  ^

Error: Cannot find module 'esm'
Require stack:
- internal/preload
    at Module._resolveFilename (node:internal/modules/cjs/loader:939:15)
    at Module._load (node:internal/modules/cjs/loader:780:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at Module._preloadModules (node:internal/modules/cjs/loader:1282:12)
    at loadPreloadModules (node:internal/bootstrap/pre_execution:582:5)
    at prepareMainThreadExecution (node:internal/bootstrap/pre_execution:96:3)
    at node:internal/main/run_main_module:7:1 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'internal/preload' ]
}

Node.js v18.3.0
commented

Cannot find module 'esm'

Install esm loader package.

it is not an ts-node API option which is used by this package

Hi, @wclr!

https://typestrong.org/ts-node/docs/options#esm

ts-node --esm

ts-node does use this option, and my test project works with this flag when using ts-node

Doesn't ts-node-dev -r esm work anyway?

Is there any update about this method?
I've tried this and the problem still persists

ts-node --esm is indeed working, but without auto-restart feature.

Any updates?
Or maybe alternatives to ts-node-dev?

I was able to get it working with ts-node and nodemon. I can't yet speak for the speed, but it works:

npm i typescript ts-node nodemon -D

# this one worked first, but there's a warning about using experimental features
nodemon -I --exec node --experimental-specifier-resolution=node  --loader ts-node/esm ./src/main.ts

# then I got this one to work, a bit shorter and easier to read, no warnings
nodemon --exec ts-node --esm -r dotenv/config src/index.ts

This was taken from this thread: #314

you can try https://github.com/esbuild-kit/tsx , it using esbuild, handle file extention (no need --experimental-specifier-resolution=node) and works like --transpile-only

I can report that tsx works like a charm! Thanks so much! The nodemon scripts from my previous comment can be reduced to this:

npm i tsx -D

tsx watch ./src/main.ts

you can try https://github.com/esbuild-kit/tsx , it using esbuild, handle file extention (no need --experimental-specifier-resolution=node) and works like --transpile-only

You are a life saver my friend, spent the last 4 hours on this