Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

NODE_PATH set in .env doesn't work #119

Closed
constfun opened this issue Jul 26, 2017 · 3 comments
Closed

NODE_PATH set in .env doesn't work #119

constfun opened this issue Jul 26, 2017 · 3 comments

Comments

@constfun
Copy link

Fresh app. Then:

echo NODE_PATH=src > .env

And change src/index.tsx

import App from './App';

to

import App from 'App';

Results in:

./src/index.tsx
(3,17): error TS2307: Cannot find module 'App'.

This is one way to get absolute imports with plain create-react-app, I tested the same process with that and it builds fine.

The .env file itself is definitely loaded, since I can set PORT, for example.

Related:
#98
facebook/create-react-app#741
facebook/create-react-app#1712
(possibly) facebook/create-react-app#2230

@constfun
Copy link
Author

constfun commented Jul 28, 2017

It seems typescript doesn't support the NODE_PATH env variable: microsoft/TypeScript#8760

Putting my local modules under src/node_modules worked for me with no other changes.

@zhenwenc
Copy link

zhenwenc commented Aug 5, 2017

I solved this issue by forking this repository, and update the webpack config. Just add paths.appSrc to the resolve.modules config (in both prod and dev) would work:

    modules: ['node_modules', paths.appNodeModules, paths.appSrc].concat(
      // It is guaranteed to exist because we tweak it in `env.js`
      process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
    ),

So that you don't need to eject, just keep your fork in sync time to time. Hope that helps :-)

@constfun
Copy link
Author

@zhenwenc So far as I can tell that achieves the same result as setting NODE_PATH=src in the .env file. process.env.NODE_PATH.split(path.delimiter).filter(Boolean)

I still don't have a solution, btw... putting code in src/node_modules is asking for trouble, so I didn't do this in the end.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants