question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.env NODE_PATH is not working in v1.1.0

See original GitHub issue

bug 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 setting NODE_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.

https://github.com/facebook/create-react-app/blob/5348d6eecf208a6298e48072314157804282002e/packages/react-scripts/config/env.js#L36-L49

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

https://github.com/facebook/create-react-app/blob/5348d6eecf208a6298e48072314157804282002e/packages/react-scripts/config/env.js#L60-L65

Solution

Manually add NODE_PATH string received from the dotenv.configure parsed property.

{ 
  parsed: { 
    NODE_PATH: 'src',
    REACT_APP_ELSE: 'foo' 
  } 
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
saurabhwahilecommented, Mar 6, 2018

I use the NODE_PATH variable for absolute imports, is there any other solution to this?

4reactions
yasharmacommented, Apr 21, 2018

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 throws Module not found: Can't resolve 'components/Nav' in '/home/yash/my-app/src', however if i run NODE_PATH=src react-scripts start it’s working, i checked by console.log(process.env) it show object { NODE_ENV:"development", PUBLIC_URL:""}

Read more comments on GitHub >

github_iconTop Results From Across the Web

"NODE_ENV" is not recognized as an internal or external ...
I wrote a module for this: win-node-env. It creates a NODE_ENV.cmd that sets the NODE_ENV environment variable and spawns a child process ...
Read more >
Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >
Command-line API | Node.js v19.3.0 Documentation
The program entry point is a specifier-like string. If the string is not an absolute path, it's resolved as a relative path from...
Read more >
API - ESBuild
This API call is used by the command-line interface if no input files are ... If needed, the working directory that esbuild uses...
Read more >
Node.js - Environment Setup - Tutorialspoint
Node.js - Environment Setup, You really do not need to set up your own environment to start learning Node.js. Reason is very simple,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found