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

.env NODE_PATH is not working in v1.1.0 #3939

Closed
molszanski opened this issue Jan 30, 2018 · 10 comments
Closed

.env NODE_PATH is not working in v1.1.0 #3939

molszanski opened this issue Jan 30, 2018 · 10 comments

Comments

@molszanski
Copy link

molszanski commented Jan 30, 2018

bug
In reference to the same issue: #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.

// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set. Variable expansion is supported in .env files.
// https://github.com/motdotla/dotenv
// https://github.com/motdotla/dotenv-expand
dotenvFiles.forEach(dotenvFile => {
if (fs.existsSync(dotenvFile)) {
require('dotenv-expand')(
require('dotenv').config({
path: dotenvFile,
})
);
}
});

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

const appDirectory = fs.realpathSync(process.cwd());
process.env.NODE_PATH = (process.env.NODE_PATH || '')
.split(path.delimiter)
.filter(folder => folder && !path.isAbsolute(folder))
.map(folder => path.resolve(appDirectory, folder))
.join(path.delimiter);

Solution

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

{ 
  parsed: { 
    NODE_PATH: 'src',
    REACT_APP_ELSE: 'foo' 
  } 
}
@molszanski
Copy link
Author

molszanski commented Jan 30, 2018

@gaearon: Thanks for confirming. Would be good if somebody could take a peek in the source into why. I'd expect it to work. #2230 (comment)

My humble findings.

@miraage
Copy link

miraage commented Jan 30, 2018

You can not override environment variables that already exist. Default Node installation exposes such env var so you can't override it. Consider using nvm. It makes Node version switching easier, also does not pollute NODE_PATH.

@molszanski
Copy link
Author

@miraage, of course! That would not be a good practice.

I should probably clarify myself. I propose expanding NODE_PATH with the results from from dotenv.

dotenvFiles.forEach(dotenvFile => {
  if (fs.existsSync(dotenvFile)) {
    var results = require('dotenv-expand')(
      require('dotenv').config({
        path: dotenvFile,
      })
    );

    if (results.parsed.NODE_PATH !== undefined) {
      process.env.NODE_PATH += results.parsed.NODE_PATH;
    }
  }
});

@gaearon
Copy link
Contributor

gaearon commented Feb 3, 2018

I don't think it makes a lot of sense to treat NODE_PATH differently from other environment variables.

I strongly recommend against setting a global NODE_PATH variable on your system.

@molszanski
Copy link
Author

It doesn't, make a lot of sense. It is just that the docs are not aligned with the feature set.

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.

I think it would be best to just remove this line from the docs and close the issue.
If it is important to anybody else, it will resurface.

Another idea, would be to add REACT_APP_FAKE_MONOREPO var that would cover a big number of use cases for NODE_PATH .env variable

@saurabhwahile
Copy link

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

@yasharma
Copy link

yasharma commented 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:""}

@Timer
Copy link
Contributor

Timer commented Apr 21, 2018

@yasharma this is the correct behavior -- the environment variable set in your shell will always win over one in a .env file.

@yasharma
Copy link

@Timer yes that's correct , however if i remove shell environment variable and want to use .env vars , its not working in that case, setting NODE_PATH=src in .env

@gedhean
Copy link

gedhean commented Jun 30, 2018

That problem caused me a big headache. Thanks for everyone. zo/

@Timer Timer closed this as completed Jul 23, 2018
@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants