'Can't run a TS node + ES app - either get `ERR_UNKNOWN_FILE_EXTENSION ` or `new ERR_MODULE_NOT_FOUND`
I'm trying to compile my code as ES code (using "type": "module"
inside package.json and "module": "esnext"
inside tsconfig.json).
I can't run it using any of the 3 approaches below:
ts-node src/server.ts
results in:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/src/server.ts
at new NodeError (node:internal/errors:363:5)
at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
at Loader.getFormat (node:internal/modules/esm/loader:105:42)
at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
at Loader.import (node:internal/modules/esm/loader:177:17)
at Object.loadESM (node:internal/process/esm_loader:68:5)
Which from this thread seems to be a problem with ts-node.
- So I try
node --loader ts-node/esm ./src/server.ts
and get this:
(node:45543) 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)
/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:383
throw new ERR_MODULE_NOT_FOUND(
^
CustomError: Cannot find module '/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/src/app' imported from /Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/src/server.ts
at finalizeResolution (/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:383:11)
at moduleResolve (/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:818:10)
at Object.defaultResolve (/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:929:11)
at /Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/src/esm.ts:68:38
at Generator.next (<anonymous>)
at /Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/dist/esm.js:8:71
at new Promise (<anonymous>)
at __awaiter (/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/dist/esm.js:4:12)
at resolve (/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/node_modules/ts-node/dist/esm.js:32:16)
at Loader.resolve (node:internal/modules/esm/loader:89:40)
But src/app.ts is clearly sitting there in the folder!
- So I tried compiling first with
tsc
and then runningnode --loader ts-node/esm ./dist/src/server.js
. I get pretty much the same error as (2), except this time it's/Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/dist/src/app
that's missing. Which also makes no sense, since I can see it in the folder.
How do I solve this? I've read every thread on stack overflow and I'm out of options. Why is running an ES node app so hard?
Solution 1:[1]
Came across the same issue. Fixed by following this ts-node issue.
TL;DR
Just add the .js
extension to your module imports and run ts-node with node --loader ts-node/esm src/index.ts
Solution 2:[2]
I had this issue, and I wanted to keep all files in typescript. I saw the answer provided by @BlueFrog but instead of converting the module imports to .js
, I used this experimental flage instead:
node --experimental-specifier-resolution=node --loader ts-node/esm myScript.ts
It works successfully.
More info here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BlueFrog |
Solution 2 | a3k |