Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: unknown file extension .ts #1062

Closed
Thomas1664 opened this issue May 30, 2020 · 78 comments
Closed

Error: unknown file extension .ts #1062

Thomas1664 opened this issue May 30, 2020 · 78 comments

Comments

@Thomas1664
Copy link

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

  • ts-node version: v8.10.2 (installed global)
  • node version: v14.3.0
  • TypeScript version:v3.9.3 (installed global)
  • tsconfig.json, if you're using one:
{}
  • Operating system and version: Windows 10 Version 2004
  • If Windows, are you using WSL or WSL2?: Yes, Ubuntu 20.04 LTS in WSL2
@Thomas1664 Thomas1664 changed the title Error: unknown file extension.ts Error: unknown file extension .ts May 30, 2020
@cspotcode
Copy link
Collaborator

ESM stuff? You might have better luck on our ESM thread. Or post a complete reproduction so someone else can debug.

@alexcochran
Copy link

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)

@Aguinaldo-Alberto-Naldo

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)

@cspotcode
Copy link
Collaborator

cspotcode commented Jun 21, 2020 via email

@clinkadink
Copy link

Remove "type": "module" from package.json

@SaintPhill
Copy link

Remove "type": "module" from package.json

If remove module type, you cannot use imports in your .ts files

@Thomas1664
Copy link
Author

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

@SaintPhill
Copy link

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

@Thomas1664
Copy link
Author

Thomas1664 commented Jul 17, 2020

I created a template repo which can be used by anyone who has trouble initializing ts-node.

@cspotcode
Copy link
Collaborator

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.

@paxperscientiam
Copy link

@clinkadink thanks, that did it for me

@cspotcode
Copy link
Collaborator

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.

@a-h
Copy link

a-h commented Jan 26, 2021

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.

@kseebrinegar
Copy link

@a-h Thank you bro! you the man!

@buzuosheng
Copy link

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

@itcat99
Copy link

itcat99 commented Apr 14, 2021

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.

@Rollingegg
Copy link

From #1007, use node --loader ts-node/esm ./my-script.ts if your tsconfig.json contains "module": "commonjs"

@shanoysinc
Copy link

I remove type: module from package json and it work

@Abeautifulsnow
Copy link

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

@iway1
Copy link

iway1 commented Jun 18, 2021

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.

@Mehak-Mehta
Copy link

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!!

@Cobular
Copy link

Cobular commented Aug 2, 2021

From #1007, use node --loader ts-node/esm ./my-script.ts if your tsconfig.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?

@goldEli
Copy link

goldEli commented Oct 8, 2021

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",  
}

@iseekTo
Copy link

iseekTo commented Oct 15, 2021

ts-node --skip-project scripts/yourFile.ts

@karlhorky
Copy link

karlhorky commented Oct 22, 2021

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:

#1007 (comment)

@cspotcode
Copy link
Collaborator

Supported by ts-node 10.3.1

@retrotechie
Copy link

Solution 1:

Remove "type": "module" in package.json

Solution 2:

package.json

...
"type": "module"
...

tsconfig.json

"compilerOptions": {
...
"module": "ESNext",
...
},
"ts-node": {
  "esm": true
}

I'm not sure if we had better solutions but these worked for me.

@rtritto
Copy link

rtritto commented Mar 18, 2023

This change should help/fix (it's released in Node v19.6.0 and there is a backport to v18).

Another related/workaround is ts-import.

@xgqfrms
Copy link

xgqfrms commented Mar 22, 2023

ts-node error ❌

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

image

ESM solution ✅

image

"type": "module", & ts-node-esm

{
  "name": "patch-package-in-action",
  "version": "1.0.0",
  "description": "patch-package in action",
  "main": "./src/app.ts",
  "type": "module",
  "scripts": {
    "app-esm": "npx ts-node-esm ./src/app.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/web-fullstack/patch-package-in-action.git"
  },
  "author": "xgqfrms",
  "license": "MIT",
  "dependencies": {
    "lodash-es": "^4.17.21",
    "typescript": "^5.0.2"
  },
  "devDependencies": {
    "@types/lodash-es": "^4.17.7",
    "app-node-env": "^1.4.7",
    "rimraf": "^4.4.0",
    "ts-node": "^10.9.1"
  }
}

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

lodash-es

// ./src/app.ts

import {toString} from "lodash-es";

type Obj = {
  [key: string]: any;
}

const obj: Obj = {
  name: 'xgqfrms',
  country: 'China',
  language: 'zh-hans',
};

const str = toString(obj);
console.log(`toString =`, str1);

refs

https://typestrong.org/ts-node/docs/imports/

@leonasdev
Copy link

I just rm -rf dist and tsc & node dist/index.js again, then it works.

@VladBrok
Copy link

VladBrok commented Jun 1, 2023

What worked for me is ts-node --esm <filename>. No esModuleInterop or package.json changes

@dfenerski thanks

@neerajkumar161
Copy link

"ts-node": {
  "esm": true
}

This worked for me. Thanks, @retrotechie 🚀

@JuYiYang
Copy link

Remove "type": "module" from package.json
remain invalid

@88brmig
Copy link

88brmig commented Aug 23, 2023

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",

Using node 20 with sveltekit, this works

@JuYiYang
Copy link

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",

Using node 20 with sveltekit, this works

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",

Using node 20 with sveltekit, this works

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",

Using node 20 with sveltekit, this works

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",

Using node 20 with sveltekit, this works

Thank you😘

@jhonatanAlanFerreira
Copy link

I need "type": "module" in package.json
npx tsx ./file.ts worked for me

@quantuminformation
Copy link

same error in 2023

anyone code a tsx launch file for vscode that allows debugging?

@arsinclair
Copy link

If you suddenly started getting this issue, this might be the cause: #2094

@ejas404
Copy link

ejas404 commented Jan 7, 2024

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.

Remove "type": "module" from package.json

If remove module type, you cannot use imports in your .ts files

for using esm add these
"compilerOptions": {

"module": "CommonJS",                                
"esModuleInterop": true,

}

it worked for me
if you face importing .ts extension

add this to your compilerOptions
"allowImportingTsExtensions": true,

Nb : Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.ts
you have to set one of the true with it

@arsinclair
Copy link

arsinclair commented Jan 8, 2024

for using esm add these

"module": "CommonJS",

That's not true. ESM and CJS (CommonJS) are two different things, changing module to CommonJS will make tsc produce CommonJS code.

@jamesrr39
Copy link

I got this same error (Unknown file extension ".ts"), trying to run a typescript script with ts-node 10.9.1 and Node 20.10. I couldn't find a solution unfortunately (I tried "esModuleInterop": true in tsconfig), but running the script through tsx worked, so I ended up migrating from ts-node to tsx.

I find it really odd that .ts files are not recognised by default. Surely ts-node should know how to handle .ts files?

@Rush
Copy link

Rush commented Jan 11, 2024

This is what worked for me. Not even ts-node-esm worked.

node --experimental-specifier-resolution=node --loader ts-node/esm file.ts

Let me just say that the entire ESM situation in Node.JS is a catastrophe. It created so much pain, incompatibility, lost hours. I don't understand why some decisions were made that contributed to this pain. It's probably not as bad as Python2 and Python3 but very close!

gordonsyme added a commit to gordonsyme/circleci-config-sdk-ts that referenced this issue Jan 16, 2024
kodiakhq bot pushed a commit to shadcn-ui/ui that referenced this issue Jan 28, 2024
…1977)

This pull request resolves #1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in #1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on #1937. That will not happen again.
@C0D3O
Copy link

C0D3O commented Feb 22, 2024

for me worked the opposite ( updated node to the latest 21.x version )
the ts-node config part in tsconfig.json

"ts-node": {
		"esm": false,
		"compilerOptions": {
			"module": "NodeNext"
		}
	},

@eminoda
Copy link

eminoda commented Apr 12, 2024

tsconfig.json

{
  "compilerOptions": {
    "module": "ESNext", // tell tsc complier use esm module
    "moduleResolution": "Node10", // find file use CommonJS require
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "outDir": "dist"
  },
  "include": ["src/**/*"]
}

package.json

"type": "module",

frontbest0726 added a commit to frontbest0726/shadcn-ui that referenced this issue Apr 26, 2024
…#1977)

This pull request resolves #1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in #1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on #1937. That will not happen again.
ShinyFrontDev added a commit to ShinyFrontDev/Radix-ui-Tailwind-CSS that referenced this issue May 4, 2024
…#1977)

This pull request resolves #1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in #1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on #1937. That will not happen again.
@hellznrg
Copy link

hellznrg commented Jun 4, 2024

I got this same error (Unknown file extension ".ts"), trying to run a typescript script with ts-node 10.9.1 and Node 20.10. I couldn't find a solution unfortunately (I tried "esModuleInterop": true in tsconfig), but running the script through tsx worked, so I ended up migrating from ts-node to tsx.

I find it really odd that .ts files are not recognised by default. Surely ts-node should know how to handle .ts files?

Switching to tsx was the only thing that worked for me. Now it's as simple as yarn tsx . and BOOM. Not sure what the problem with ts-node is. It's defintely a ts-node problem.

kjxbyz pushed a commit to muse-ui/muse-ui that referenced this issue Jun 7, 2024
…hadcn-ui#1977)

This pull request resolves shadcn-ui#1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in shadcn-ui#1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on shadcn-ui#1937. That will not happen again.
magicdragon1101 added a commit to magicdragon1101/shadcn-ui that referenced this issue Jun 27, 2024
…#1977)

This pull request resolves #1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in #1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on #1937. That will not happen again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests