.env NODE_PATH is not working in v1.1.0
See original GitHub issuebug In reference to the same issue: https://github.com/facebook/create-react-app/issues/2230 NODE_PATH is ignored when defined in .env file.
And that contradicts the latest docs.
You can adjust various development and production settings by setting environment variables in your shell or with .env. NODE_PATH: Same as
NODE_PATH
in Node.js, but only relative folders are allowed. Can be handy for emulating a monorepo setup by settingNODE_PATH=src
.
Why does it happen?
It happens because NODE_PATH should be configured when config/env.js
executes.
And dotenv
module does not modify existing variables.
We will never modify any environment variables that have already been set. In particular, if there is a variable in your
.env
file which collides with one that already exists in your environment, then that variable will be skipped.
So the code will simply fail to append NODE_PATH from an .env
file to process.env.NODE_PATH
variable that is used to provide this feature
Solution
Manually add NODE_PATH string received from the dotenv.configure
parsed
property.
{
parsed: {
NODE_PATH: 'src',
REACT_APP_ELSE: 'foo'
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
I use the NODE_PATH variable for absolute imports, is there any other solution to this?
setting
NODE_PATH=src
in .env is not working for me, create-react-app version 1.5.2, i’ve tried to import components like'components/Nav'
, however it throwsModule not found: Can't resolve 'components/Nav' in '/home/yash/my-app/src'
, however if i runNODE_PATH=src react-scripts start
it’s working, i checked byconsole.log(process.env)
it show object{ NODE_ENV:"development", PUBLIC_URL:""}