Closed Thomas1664 closed 4 years ago
ESM stuff? You might have better luck on our ESM thread. Or post a complete reproduction so someone else can debug.
I'm having the same issue.
λ npx ts-node src/app.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\$USER\$PATH_TO_DEV_FOLDER\src\app.ts
at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
at Loader.getFormat (internal/modules/esm/loader.js:113:42)
at Loader.getModuleJob (internal/modules/esm/loader.js:243:31)
at Loader.import (internal/modules/esm/loader.js:177:17)
Estou tendo o mesmo problema também
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for www\server\src\server.ts at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15) at Loader.getFormat (internal/modules/esm/loader.js:113:42) at Loader.getModuleJob (internal/modules/esm/loader.js:243:31) at Loader.import (internal/modules/esm/loader.js:177:17)
My earlier suggestion should point you in the right direction.
On Sun, Jun 21, 2020, 9:44 AM AguinaldoAlberto notifications@github.com wrote:
Estou tendo o mesmo problema também
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for www\server\src\server.ts at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15) at Loader.getFormat (internal/modules/esm/loader.js:113:42) at Loader.getModuleJob (internal/modules/esm/loader.js:243:31) at Loader.import (internal/modules/esm/loader.js:177:17)
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/TypeStrong/ts-node/issues/1062#issuecomment-647130364, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC35OCO5M3WUUNXSLA4YS3RXYFCVANCNFSM4NOUHHYQ .
Remove "type": "module" from package.json
Remove "type": "module" from package.json
If remove module type, you cannot use imports in your .ts files
I must disagree. In Angular projects, there is no "type": "module" in package.json and import synthax works fine. Try to set "module": "es2020" in tsconfig.json
I must disagree. In Angular projects, there is no "type": "module" in package.json and import synthax works fine. Try to set "module": "es2020" in tsconfig.json
nope, same problem with import. "Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension."
I created a template repo which can be used by anyone who has trouble initializing ts-node
.
Closing as this is not a ts-node
bug. If you want to use node's native ESM support, which is currently experimental, then you can use ts-node
's experimental ESM loader hook. See #1007 for details.
@clinkadink thanks, that did it for me
We've enabled the discussion forum for questions like this one.
https://github.com/TypeStrong/ts-node/discussions
We're trying to keep the issue tracker limited to actionable tasks, so contributors can focus on making fixes and improvements to ts-node
.
Since this is not a bug and nothing needs to change in ts-node
, it's not actionable, so it was closed. Discussions, on the other hand, are the perfect place to ask advice, share guides, and help with project configuration.
Another good thing about the discussion forum: you don't need to fill out an issue template.
This stack overflow answer helped me: https://stackoverflow.com/questions/63445821/ts-node-execute-typescript-with-module-import-and-module-defined
Updating my tsconfig.json to migrate from "module": "es6"
to "module": "commonjs"
allowed me to run my TypeScript file.
@a-h Thank you bro! you the man!
Remove "type": "module" from package.json
If remove module type, you cannot use imports in your .ts files
I'm having the same issue. how to fix, thx
This stack overflow answer helped me: https://stackoverflow.com/questions/63445821/ts-node-execute-typescript-with-module-import-and-module-defined
Updating my tsconfig.json to migrate from
"module": "es6"
to"module": "commonjs"
allowed me to run my TypeScript file.
Thanks! it's useful.
From #1007, use node --loader ts-node/esm ./my-script.ts
if your tsconfig.json
contains "module": "commonjs"
I remove type: module from package json and it work
This stack overflow answer helped me: https://stackoverflow.com/questions/63445821/ts-node-execute-typescript-with-module-import-and-module-defined
Updating my tsconfig.json to migrate from
"module": "es6"
to"module": "commonjs"
allowed me to run my TypeScript file.
This solution resolves my two problems which are "import" syntax-error outside module and unknown file extensions ".ts"!!! Thank you very much for saving my time. 😁
Add this to the tsconfig.json:
"compilerOptions": {
"esModuleInterop": true
}
and make sure to start the server with ts-node
:
ts-node my_ts_node_server.ts
It is crazy how hard it was for me to find this. This will allow you to use imports as well as use typescript.
Add this to the tsconfig.json:
"compilerOptions": { "esModuleInterop": true }
and make sure to start the server with
ts-node
:ts-node my_ts_node_server.ts
It is crazy how hard it was for me to find this. This will allow you to use imports as well as use typescript.
Yup that works!!
From #1007, use
node --loader ts-node/esm ./my-script.ts
if yourtsconfig.json
contains"module": "commonjs"
Real quick follow up to this - running with this makes my code ~8x slower than ts-node
by itself with changed options or just using tsc
and node
, and that's testing the same exact function 10 times in a loop to mitigate the effects of the typescript JIT compilation times. Hopefully this is useful to someone?
Remove "type": "module" from package.json
If remove module type, you cannot use imports in your .ts files
set the value of module to commonjs
in tsconfig.json, it works for me.
{
"module": "commonjs",
}
ts-node --skip-project scripts/yourFile.ts
For anyone who is running into this issue on CI or on Node.js 16.12.0, it seems like this latest version has made some changes to the hooks API 😱
https://github.com/nodejs/node/releases/tag/v16.12.0
Also mentioned here:
https://github.com/TypeStrong/ts-node/issues/1007#issuecomment-948484016
Supported by ts-node 10.3.1
This is what worked for me ts-node: 10.2.1 typescript: 4.4.2 node: 16.8.0
in package.json I removed "target": "module"
in tsconfig.json I added this
"ts-node": { "compilerOptions": { "module": "commonjs" }
an in a separate "compilerOptions" "module": "ESNEXT"
I was getting a different error which is "Cannot use import statement outside a module" as a suggested solution in stack overflow I added "target":"module" in package.json which lead me to this error: unknown file extension .ts
Hope it helps you out. I also found this article https://www.tsmean.com/articles/learn-typescript/typescript-module-compiler-option/
I spent an hour banging my head against a wall this morning trying to figure this out - if you have a dependency in your project which uses "type": "module"
in it, it can also cause this problem. In my case, I had node-fetch@3.1.0
. Downgrading to node-fetch@2.5.12
fixed it for me.
I spent an hour banging my head against a wall this morning trying to figure this out - if you have a dependency in your project which uses "type": "module" in it, it can also cause this problem. In my case, I had node-fetch@3.1.0. Downgrading to node-fetch@2.5.12 fixed it for me.
After hours lost this is the "solution". Thanks.
I spent an hour banging my head against a wall this morning trying to figure this out - if you have a dependency in your project which uses
"type": "module"
in it, it can also cause this problem. In my case, I hadnode-fetch@3.1.0
. Downgrading tonode-fetch@2.5.12
fixed it for me.
What if you can not do that :)? That's not a solution.
The --loader is sometimes necessary. There are myriad issues where this is discussed, plus node's documentation. When searching, be sure to check the discussion forum and include closed issues in your search. Pull requests which improve documentation are always welcome if you feel that is appropriate.
@Stevemoretz Unfortunately I think the issue is in node-fetch's end. I tried every configuration after losing an hour to this mindlessness and I'm not planning on wasting more time on it
@Stevemoretz Unfortunately I think the issue is in node-fetch's end. I tried every configuration after losing an hour to this mindlessness and I'm not planning on wasting more time on it
Me too unfortunately I revered back to js for now.
If you think node-fetch is the issue, you need to downgrade to a pre-3.0.0 version. In 3.0.0, they changed the module to be ESM-only. It no longer exports a CJS version. "chalk" is another package that did this, starting with 5.0.0.
I'd like to report some success just for any lost soul who's found there way here. I've discovered the below works fine for me–and mind you, this is "all modern" Typescript output as well as the Node "type": "module"
use case, so YMMV but it certainly works with all of @sindresorhus's newest ESM-only packages (find-up
, chalk
etc.). Seems to also work for standard-issue CommonJS packages.
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "esnext",
"rootDir": "./src",
"moduleResolution": "node",
"types": ["node"],
"resolveJsonModule": true,
"outDir": "./dist/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["./src/**/*.ts"]
}
package.json
{
// ...
"type": "module"
// ...
}
and the ts-node
command: node --experimental-specifier-resolution=node --loader ts-node/esm src/main.ts
This also works with tsc
and node
, you just need to be sure to specify the same resolution mode. So, something along the lines of tsc && node --experimental-specifier-resolution=node dist/main.js
will work.
Node version I'm using right now is v14.17.3
–probably need to update but newer versions of NPM have issues with the corporate Artifactory 🥲
I am trying to use Typescript from Webpack but latest Angular updated their webpack plugins to ESM. :D Eh, the ESM is such a disaster that one can only laugh. So many hours needlessly wasted. Unfortunately none of the solutions in this thread help.
This is the solution for me, hope it helps someone I'm using Express 4.17.3, Node 14.17.0
{
"name": "xxx",
"version": "1.0.0",
"main": "index.js",
"author": "xxx",
"license": "MIT",
"scripts": {
"ts-check": "tsc --noemit",
},
"dependencies": {
"express": "^4.17.3",
"tslib": "^2.3.1"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.23",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
}
}
{
"compileOnSave": false,
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"baseUrl": ".",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSources": true,
"isolatedModules": true,
"lib": ["dom", "dom.iterable", "esnext"],
"module": "commonjs",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"paths": {
"@routes/*": ["src/routes/*"],
"@config/*": ["src/config/*"],
"@controllers/*": ["src/controllers/*"],
"@models/*": ["src/models/*"],
"@utils/*": ["src/utils/*"],
"@socket/*": ["socket/*"],
"@src/*": ["src/*"],
},
"preserveConstEnums": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es5",
"removeComments": true,
"importHelpers": true,
"typeRoots": ["./node_modules/@types"],
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "dist"]
}
Following this ts-node documentation CommonJS vs native ECMAScript modules worked for me.
I must disagree. In Angular projects, there is no "type": "module" in package.json and import synthax works fine. Try to set "module": "es2020" in tsconfig.json
literally what does angular have to do with node??? doesn't matter if you're using babel. the question is about using typescript with node so thank you for reading and understanding the question carefully in order to provide constructive and helpful guidance. Great job author!
No matter what I do, I can't get beyond "Unknown file extension ".ts"". My project is in TypeScript, and I'm trying to import an npm package (double-metaphone) that only working as an ESM import. Since I'm trying run my code using:
node --experimental-specifier-resolution=node --loader ts-node/esm app.ts
...it seems utterly bizarre that I would get an error complaining about the ".ts" extension. What other than ".ts" would the ts-node loader expect?
I've tried various combinations and permutations of suggestion here and elsewhere. My latest setup, running under Node.js v16.13.1, looks like this:
{
"name": "my-project",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node --experimental-specifier-resolution=node --loader ts-node/esm app.ts",
},
"dependencies": {
// blah, blah, blah
"double-metaphone": "^2.0.0",
// blah, blah, blah
},
"devDependencies": {
// blah, blah, blah
"ts-node": "^10.0.0",
"typescript": "^4.5.4"
}
}
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"sourceMap": true,
"outDir": "./tsc-out",
"noImplicitAny": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noImplicitReturns": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2020",
"dom"
]
},
"ts-node": {
"esm": true
}
}
Specifying the loader via NODE_OPTIONS
did work form me, even though I had "esm": true
in my tsconfig.json:
NODE_OPTIONS="--loader ts-node/esm" node ./index.ts
@zirkelc That is because esm:true must spawn a subprocess to pass --loader
and it can't do that when you run node ./index.ts
directly.
I added "type": "module" in package.json and did npx tsx file.ts
instead of using ts-node file.ts
and it worked.
You probably needed to enable "esm": true
if your tsconfig.json contains "module": "ESNext". I have used the below script in my package.json. It worked.
"start": "nodemon -e ts -w ./src -x npm run watch:serve",
"watch:serve": "node --loader ts-node/esm src/server.ts",
To all the poor souls still battling this like me: I might have a solution with ESM, "type: "module" and ts-node for VSCode. It involves adding a --loader runtime arg to launch.json. I hope it helps!
(Windows, Node 18.2.0)
package.json:
{
...
"main": "source/main.js",
"type": "module",
"devDependencies": {
"@types/node": "^18.6.1",
"ts-node": "^10.9.1"
}
}
tsconfig.json:
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 16",
"compilerOptions":
{
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": ["es2020"],
"module": "ESNext",
"outDir": "output",
"skipLibCheck": true,
"strict": true,
"target": "es2020"
},
"include": ["source"],
"exclude": ["node_modules"],
"ts-node":
{
"esm": true // from the top of https://typestrong.org/ts-node/docs/imports/
}
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "TS Debug",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeArgs": ["-r", "ts-node/register", "--loader", "ts-node/esm"],
"args": ["${workspaceRoot}/source/runme.ts"]
}
]
}
With this, I can use ESM import/export, run the VSCode debugger, catch breakpoints in my TS files, etc...
NOTE: I have to use import paths with .js filenames, which seems odd, but works. The actual included file is .ts, and the debugger steps into the .ts file. Example: import * as JOE from './joelib.js'; works with my file "./joelib.ts".
What worked for me is ts-node --esm <filename>
. No esModuleInterop
or package.json
changes
I spent an hour banging my head against a wall this morning trying to figure this out - if you have a dependency in your project which uses "type": "module" in it, it can also cause this problem. In my case, I had node-fetch@3.1.0. Downgrading to node-fetch@2.5.12 fixed it for me.
After hours lost this is the "solution". Thanks.
It is actually the solution :D
Use ts-node --esm
, ts-node-esm
, or set "esm": true
in tsconfig.json
remove type: "module" and add in your tsconfig.json "compilerOptions":{ "module": "Commonjs", "moduleResolution": "node" }
Expected Behavior
Load typescript files
Actual Behavior
When I try to execute a typescript file I get the following error message: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/$USERNAME/$PATH_TO_DEV_FOLEDER/index.ts at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15) at Loader.getFormat (internal/modules/esm/loader.js:113:42) at Loader.getModuleJob (internal/modules/esm/loader.js:243:31) at Loader.import (internal/modules/esm/loader.js:177:17)
Steps to reproduce the problem
launch ts-node index.ts
Minimal reproduction
Specifications