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

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

Open
loynoir opened this issue Apr 24, 2023 · 45 comments
Open

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

loynoir opened this issue Apr 24, 2023 · 45 comments

Comments

@loynoir
Copy link

loynoir commented Apr 24, 2023

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 loynoir changed the title TypeError: value.replace is not a function [Typescript 5.0] TypeError: value.replace is not a function Apr 24, 2023
@loynoir
Copy link
Author

loynoir commented Apr 24, 2023

Workaround

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

@basicdays
Copy link

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.

@mrkjdy
Copy link

mrkjdy commented May 2, 2023

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.

@muratgozel
Copy link

Yep, this is kind of urgent for me too 😞

@sebaplaza
Copy link

Any news on this ?

@duckboy81
Copy link

duckboy81 commented May 12, 2023

Workaround
May have to install direct from git until the next release, see #1958

From #1977 (comment)

npm install -D TypeStrong/ts-node#main

@gspetrou
Copy link

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

@liambutler-lawrence
Copy link

liambutler-lawrence commented May 24, 2023

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?

@StevenDouillet
Copy link

StevenDouillet commented Jun 7, 2023

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

@andykenward
Copy link

andykenward commented Jun 7, 2023

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 #2000 (comment)

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"

@guoyunhe
Copy link

Same here. It is breaking everything.

@sebaplaza
Copy link

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

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

@gspetrou
Copy link

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

@sebaplaza
Copy link

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

@chenxxzhe
Copy link

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

@TheCleric
Copy link

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

This worked for me as well. Thanks!

@Kurt-von-Laven
Copy link

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

@ahollenbach
Copy link

FYI - this fix has been made already (confirmed locally): #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: #1977

@binarykitchen
Copy link

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

@Kurt-von-Laven
Copy link

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.

@muratgozel
Copy link

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

no

@adrian-gierakowski
Copy link

@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
Copy link

@gspetrou does it support composite TS projects with project references? Thanks!

@giseburt
Copy link

@muratgozel here's something that does type check and has been updated to support language features released in the last year

https://www.npmjs.com/package/tsx

I don’t know why you’d say that. tsx does not type check. Says so right on the page linked to. 🤦‍♂️

@top-kat
Copy link

top-kat commented Jan 7, 2024

https://www.npmjs.com/package/tsx was the best choices for me! I had a lot of pain with ts-node, and either vite-node which was better BTW

@JeffJacobson
Copy link

https://www.npmjs.com/package/tsx was the best choices for me! I had a lot of pain with ts-node, and either vite-node which was better BTW

Thanks. This worked for me. They really need to fix this issue in this package, though.

@rockey2020
Copy link

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

jason-ha added a commit to microsoft/FluidFramework that referenced this issue Mar 13, 2024
and cleanup test script spec

Note: test:mocha-ts* are not usable per no ts-node release to address TypeStrong/ts-node#2000 where tsconfig extends multiple files.
jason-ha added a commit to microsoft/FluidFramework that referenced this issue Mar 13, 2024
test:mocha-ts* are not usable per several issues with ts-node (10.9.2 or earlier):
- TypeStrong/ts-node#2000 no tsconfig extends multiple files support
- TypeStrong/ts-node#1967 lack of .ts + ESM understanding
- failure to resolve .js extensions using CommonJS
jason-ha added a commit to microsoft/FluidFramework that referenced this issue Mar 13, 2024
- fix explicit paths for full ESM
- address lodash imports
- various corrections from more strict tsc options with tests
   including testing LZ4PropertyTree not previously tested.

## remove mocha-ts scripts
test:mocha-ts* are not usable per several issues with ts-node (10.9.2 or
earlier):
- TypeStrong/ts-node#2000 no tsconfig extends
multiple files support
- TypeStrong/ts-node#1967 lack of .ts + ESM
understanding
- failure to resolve .js extensions using CommonJS

---------

Co-authored-by: Sonali Deshpande <[email protected]>
Co-authored-by: Jason Hartman <[email protected]>
@adi518
Copy link

adi518 commented Mar 24, 2024

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

But the whole point is to be able to extend from multiple configs. That workaround is only good for those inheriting from just one config.

@gspetrou
Copy link

gspetrou commented Mar 24, 2024

But the whole point is to be able to extend from multiple configs. That workaround is only good for those inheriting from just one config.

Are you saying swc-node errors when the extends field of tsconfig.json is an array? I don't think that is true.


By the way, congrats on having this issue fixed for over a year now in main without releasing it in npm 👏🏼👏🏼👏🏼

@adi518
Copy link

adi518 commented Mar 24, 2024

No, I misquoted, see edit.

@adamspotlite
Copy link

heyo, any update for the best way to handle this? using in an expo project in an app.config.ts - docs say to use this technique but getting this error :/

@ndinata
Copy link

ndinata commented Apr 15, 2024

To anyone running into this issue on Expo from trying to import a TS file in app.config.ts: if you could afford an alternative, a solution that worked for me was to replace ts-node with @swc-node/register and register that instead at the top of my app.config.ts.

// app.config.ts

import "@swc-node/register";  // <-- insert this line

import { SomeValue } from "./your-ts-module";

export default ({ config }: ConfigContext): ExpoConfig => ({
  ...
});

@david1542
Copy link

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

OMG it worked

@mp3por
Copy link

mp3por commented Apr 21, 2024

WHY IS THIS NOT AVAILABLE THROUGH NPM ?

@top-kat
Copy link

top-kat commented Apr 21, 2024

@mp3por Please don't use ts-node, it's not maintained and actually there is much better solutions right now.
Too bad that there is not a message about that in the readme. It's really not correct to let so much peoples in troubles I really agree.
Actually I use bun, tsx is also a good alternative.

@mp3por
Copy link

mp3por commented Apr 21, 2024

@top-kat thank you - however nodemon uses ts-node. I don't know how to configure it otherwise. All I want is to auto compile and restart my TS app on file change

@jensbodal
Copy link

jensbodal commented Apr 21, 2024

@top-kat thank you - however nodemon uses ts-node. I don't know how to configure it otherwise. All I want is to auto compile and restart my TS app on file change

I use nodemon with bun and tsx all the time

nodemon -w file.ts -x "bun file.ts" 

For example. Of course can use bun or tsx directly for this exact use case, but there's times I want to use nodemon for the sake of monitoring other files to initiate the rebuild.

If you're watching multiple typescript files or different types of files you have to use the -e flag (e.g. -e ts,js,json)

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