ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

[Typescript 5.0] TypeError: value.replace is not a function

Open loynoir opened this issue 1 year ago β€’ 45 comments

Search Terms

  • Typescript 5.0
  • tsconfig extends array

Expected Behavior

No error.

Actual Behavior

$ npm exec -- ts-node --esm --swc ./path/to/reproduce.mts
/path/to/node_modules/ts-node/dist/util.js:62
    return value.replace(backslashRegExp, directorySeparator);
                 ^

TypeError: value.replace is not a function
    at normalizeSlashes (/path/to/node_modules/ts-node/dist/util.js:62:18)
    at Object.getExtendsConfigPath (/path/to/node_modules/ts-node/dist/ts-internals.js:24:54)
    at readConfig (/path/to/node_modules/ts-node/dist/configuration.js:127:64)
    at findAndReadConfig (/path/to/node_modules/ts-node/dist/configuration.js:50:84)
    at phase3 (/path/to/node_modules/ts-node/dist/bin.js:254:67)
    at bootstrap (/path/to/node_modules/ts-node/dist/bin.js:47:30)
    at Object.<anonymous> (/path/to/node_modules/ts-node/dist/child/child-entrypoint.js:23:21)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)

Node.js v18.15.0

Debug

function normalizeSlashes(value) {console.warn({value})
    return value.replace(backslashRegExp, directorySeparator);
}

errrlog

{
  value: [ '@tsconfig/strictest/tsconfig', '@tsconfig/node18/tsconfig' ]
}

Steps to reproduce the problem

Minimal reproduction

Specifications

  • ts-node version: latest
  • node version: v18.15.0
  • TypeScript version: latest
  • tsconfig.json, if you're using one:
  "extends": [
    "@tsconfig/strictest/tsconfig",
    "@tsconfig/node18/tsconfig"
  ],
  • package.json:
{}
  • Operating system and version:
  • If Windows, are you using WSL or WSL2?:

Additional

https://www.npmjs.com/package/@tsconfig/node18-strictest-esm

This package has been deprecated Author message: TypeScript 5.0 supports combining TSConfigs using array syntax in extends

https://github.com/tsconfig/bases#what-about-combined-configs

Because of previous limitations in the config extension system of TypeScript, this repo used to provide combined configs from a few common bases (like Node + ESM, Node + Strictest and so on).

This issue is now moot since TypeScript v5.0.0, which provides the ability to extend from multiple configs at once. For instance, if you want to start from a Node 18 + Strictest base config, you can install both @tsconfig/node18 and @tsconfig/strictest packages and extend those configs like so:

// tsconfig.json
{
  "extends": ["@tsconfig/strictest/tsconfig", "@tsconfig/node18/tsconfig"]
}

https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-rc/#supporting-multiple-configuration-files-in-extends

loynoir avatar Apr 24 '23 03:04 loynoir

Workaround

$ npm exec -- tsc --showConfig > tsconfig.tsnode.json
$ npm exec -- ts-node --project tsconfig.tsnode.json --esm --swc ./reproduce.mts

loynoir avatar Apr 24 '23 03:04 loynoir

Note that if you have a ts-node config section in your tsconfig file, that it won't be ported into your tsc --showConfig output. You'll need to manually copy that over after the hacked tsconfig file is created.

basicdays avatar Apr 26 '23 20:04 basicdays

It would be awesome if ts-node could release a fix for this! A package that I use (Cypress) uses ts-node internally, and the old @tsconfigs in the projects that I work on are incompatible with TypeScript 5 because they have some options that are no longer supported. So until ts-node is fixed and Cypress uses that new version, the projects that I work on will have to stick with TypeScript 4 and the old @tsconfigs.

mrkjdy avatar May 02 '23 15:05 mrkjdy

Yep, this is kind of urgent for me too 😞

muratgozel avatar May 02 '23 16:05 muratgozel

Any news on this ?

sebaplaza avatar May 10 '23 08:05 sebaplaza

Workaround May have to install direct from git until the next release, see https://github.com/TypeStrong/ts-node/pull/1958

From https://github.com/TypeStrong/ts-node/discussions/1977#discussion-5014427

npm install -D TypeStrong/ts-node#main

duckboy81 avatar May 12 '23 09:05 duckboy81

Can we please get a 10.9.2 with just this fix? Is there any ETA? What is the benefit of waiting?

gspetrou avatar May 22 '23 17:05 gspetrou

Agreed. It seems absurd that a primary package in the TS ecosystem hasn't released an update (that's already merged) to support the current version of the TS language. Is this project no longer being actively maintained?

liambutler-lawrence avatar May 24 '23 22:05 liambutler-lawrence

Does someone have a workaround about this issue ? It still blocking me while running end to end and unit tests 😞

And i've got the same issue when i try to get the next dev release

My workarround : leave only one element on the extends ("extends": "./node_modules/gts/tsconfig-google.json") and move others in the main tsconfig file

StevenDouillet avatar Jun 07 '23 14:06 StevenDouillet

Does someone have a workaround about this issue ? It still blocking me while running end to end and unit tests 😞

And i've got the same issue when i try to get the next dev release

My workarround : leave only one element on the extends ("extends": "./node_modules/gts/tsconfig-google.json") and move others in the main tsconfig file

@StevenDouillet did you try the workaround by @loynoir https://github.com/TypeStrong/ts-node/issues/2000#issuecomment-1519339593

It outputs a tsconfig.tsnode.json from your tsconfig.json that ts-node can understands.

If it helps I've used it in this repo unlike-ltd/cloudflare-pages-action

I have 2 cli scripts that use it.

    "tsc:ts-node-config": "tsc --showConfig > tsconfig.tsnode.json",
    "ts-node": "ts-node-esm --project tsconfig.tsnode.json",

Then for any other script I do

    "dev": "pnpm run ts-node --require dotenv/config src/index.ts",
    "codegen": "pnpm run ts-node -T bin/codegen/index.ts",
    "download": "pnpm run ts-node -T bin/download/index.ts"

andykenward avatar Jun 07 '23 15:06 andykenward

Same here. It is breaking everything.

guoyunhe avatar Jun 24 '23 11:06 guoyunhe

As a definitive solution, you can migrate to @swc/register.

Also, It can be easily used by jest, mocha, etc...

sebaplaza avatar Jun 26 '23 13:06 sebaplaza

As a definitive solution, you can migrate to @swc/register.

Also, It can be easily used by jest, mocha, etc...

In what way is this a solution? It looks like this just binds SWC to be used when importing files with require. This is unrelated to the owner of ts-node refusing to release bug fixes for no reason

gspetrou avatar Jun 26 '23 14:06 gspetrou

@gspetrou the solution is to migrate to another library (like @swc/register).

This is a critical issue, in a critical library (in ts ecosystem) and there is still no solution (the issue was created on April 24)

There is nothing we can do to fix this issue, other than wait for the maintainers.

sebaplaza avatar Jun 27 '23 08:06 sebaplaza

In my project , change tsconfig.json extends: ['...'] to extends: '...' can fix it.

chenxxzhe avatar Jul 11 '23 06:07 chenxxzhe

In my project , change tsconfig.json extends: ['...'] to extends: '...' can fix it.

This worked for me as well. Thanks!

TheCleric avatar Jul 21 '23 18:07 TheCleric

@chenxxzhe, the reason for the thumbs down is that the whole point of this issue is that folks are hoping for support for extends: ['...'].

Kurt-von-Laven avatar Aug 05 '23 01:08 Kurt-von-Laven

FYI - this fix has been made already (confirmed locally): https://github.com/TypeStrong/ts-node/pull/1958. However, the maintainers have not issued a new release in more than a year and are recommending installing via git branch in order to consume this change: https://github.com/TypeStrong/ts-node/discussions/1977

ahollenbach avatar Aug 14 '23 19:08 ahollenbach

@ahollenbach What's the reason why the maintainers haven't issued a new release for more than one year?

binarykitchen avatar Sep 27 '23 19:09 binarykitchen

I would consider porting to an alternative like SWC if you require TypeScript v5 support. Maintainers of open-source repositories don't owe their users answers to questions like this.

Kurt-von-Laven avatar Sep 28 '23 08:09 Kurt-von-Laven

Or just fork it and publish it yourself since it's open source and contains changes other people than just the maintainers committed to benefit everyone

gspetrou avatar Sep 28 '23 11:09 gspetrou

FYI - the beta version of ts-node 11 is published: https://www.npmjs.com/package/ts-node/v/11.0.0-beta.1 so installing via git is no longer necessary.

It can be installed using npm install [email protected] if using npm.

ahollenbach avatar Nov 30 '23 13:11 ahollenbach

With [email protected] and TS >5 I'm still getting the same errors with extends. My only way to fix it is to downgrade to TS 4.9.

Bloodsucker avatar Nov 30 '23 14:11 Bloodsucker

With [email protected] and TS >5 I'm still getting the same errors with extends. My only way to fix it is to downgrade to TS 4.9.

@Bloodsucker I was too, with Jest, until I added overrides:

  "devDependencies": {
    "ts-node": "11.0.0-beta.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "5.2.2"
  },
  "overrides": {
    "ts-node": "11.0.0-beta.1",
    "tsconfig-paths": "4.2.0"
  }

therockstorm avatar Nov 30 '23 14:11 therockstorm

With [email protected] and TS >5 I'm still getting the same errors with extends. My only way to fix it is to downgrade to TS 4.9.

@Bloodsucker I was too, with Jest, until I added overrides:

  "devDependencies": {
    "ts-node": "11.0.0-beta.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "5.2.2"
  },
  "overrides": {
    "ts-node": "11.0.0-beta.1",
    "tsconfig-paths": "4.2.0"
  }

Just an FYI so you don't need to duplicate the version, you can just do

  "devDependencies": {
    "ts-node": "11.0.0-beta.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "5.2.2"
  },
  "overrides": {
    "ts-node": "$ts-node",
    "tsconfig-paths": "$tsconfig-paths"
  }

reference

jensbodal avatar Dec 01 '23 18:12 jensbodal

the 11.0.0-beta.1 release is affected by this bug when used with TypeScript 5.3.3

adrian-gierakowski avatar Dec 11 '23 13:12 adrian-gierakowski

I switched to swc guys, its pretty simple:

node --loader @swc-node/register/esm src/index.ts

Besides, wanted to share, just saw node has a --watch flag now. so with one less dependency, my npm dev command is even more beautiful:

node -r dotenv/config --watch --watch-path src --loader @swc-node/register/esm src/index.ts

muratgozel avatar Dec 23 '23 10:12 muratgozel

@muratgozel does swc type check the code before running it?

adrian-gierakowski avatar Dec 23 '23 12:12 adrian-gierakowski

@muratgozel does swc type check the code before running it?

no

muratgozel avatar Dec 23 '23 12:12 muratgozel

@muratgozel does swc type check the code before running it?

no

Then it’s not a like for like replacement for ts-node.

adrian-gierakowski avatar Dec 23 '23 12:12 adrian-gierakowski