Create-react-app: Fix `baseUrl` for `NODE_PATH=src`

Created on 2 Nov 2018  路  11Comments  路  Source: facebook/create-react-app

title

bug typescript

Most helpful comment

Yes, this will start permitting absolute imports in TS apps.

All 11 comments

Out of curiosity, would this allow absolute imports in TS apps created with create-react-app?

I've been trying to get it working and remember setting the NODE_PATH for JS projects. Loving the Typescript support 馃檹

Yes, this will start permitting absolute imports in TS apps.

Will this work with NODE_PATH=. as well?

I use this because it forces referencing src/, removing any ambiguity as to what is being imported.

Not sure if we'll allow . or not, there's always https://github.com/facebook/create-react-app/issues/5118 which will let you import via @/.

We use the same as @tkazec, so that instead of having to have a dozen different absolute paths (plus paths for files that were under src itself...I'm not sure how src/App.ts would be referenced without the src prefix present) we only had one, and thus had a lot less risk of a name clash between one of our folders and an npm package name.

@niekert
If you're looking for a way to enable absolute path imports, see https://github.com/facebook/create-react-app/issues/5118#issuecomment-439665737

Adding "baseUrl": "./src", to my compilerOptions options in tsconfig.json worked for me.

@jamesg1 That's been working for me for over a year too, but on this new project, whenever I run yarn start, the baseUrl entry is removed from tsconfig.json!

Anyone have a way to prevent this?

@mindjuice Yeah I think there was a change which resets the baseUrl might have to use react rewired.

See https://github.com/facebook/create-react-app/issues/5118#issuecomment-464025389

@mindjuice The tsconfig is rewritten on every start. You have to use extends (https://github.com/facebook/create-react-app/issues/5645#issuecomment-461999138)

We now support setting baseUrl in tsconfig.json (and jsconfig.json). Currently you can only set it to node_modules (the default) and src. We are hoping to add support for aliases in the near future and may look at relaxing the restriction on baseUrl.

NODE_PATH is now deprecated and since you can just set baseUrl directly I think this can be closed.

Was this page helpful?
0 / 5 - 0 ratings