.env NODE_PATH is not working in v1.0.0
Description
I have NODE_PATH=src in .env, it did work great in v0.9.5. After yarn upgrade react-scripts it doesn't work now. And I didn't see any related guide in the v1.0.0 changelog.
(referenced from https://github.com/facebookincubator/create-react-app/issues/2188)
I've also tried in a fresh new project and it doesn't work.
Expected behavior
Should be able to import 'foo/bar' which foo is a folder located in src
Actual behavior
Module not found: Can't resolve 'foo/bar' in '/Users/some/path/src'
Environment
Run these commands in the project folder and fill in their results:
npm ls react-scripts(if you haven’t ejected): v1.0.0node -v: v7.1.0npm -v: v3.10.9
Then, specify:
- Operating system: macOS Sierra
- Browser and version: Chrome 58
Reproducible Demo
https://github.com/CodinCat/cra-env-issue
This is a fresh new project. See /src/index.js:5
Didn't knew you could add that to .env, I'm using it in a custom npm script and it still works fine after updating to 1.0.0:
"scripts": {
...
"dev": "cross-env NODE_PATH=./src npm run start"
}
I've got a problem with paths in tests by the way but I don't know if that's related to NODE_PATH or to the updated version of jest, I will report as soon as I could find where exactly the problem is.
This is a real deal-breaker on upgrades to apps that use this form of importing. Changing an entire app to use relative paths will be tiresome. I tried @ingro's suggestion but that didn't work for me; still getting module not found errors.
Interesting. Anyone looked yet into why this happens?
Same for me. Have NODE_PATH=./ in .env and imports like src/components not working anymore.
Thanks for confirming. Would be good if somebody could take a peek in the source into why. I'd expect it to work.
Seems unrelated to ModuleScopePlugin. Will investigate more.
As stated above I have only problems with tests, moving the __tests__ folder from the root to src (since now only tests inside src will run) returns me this weird error (my tests also relies on NODE_PATH):
FAIL src\__tests__\modules\stats\stats.test.js
● Test suite failed to run
Cannot find module 'package.json'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
If I change imports in tests to be relative the error disappear.
This doesn't seem related to jest because I created a small repo to try to reproduce this error without cra but it's working fine there.
Can you show the test code? Does it import package.json?
Looks like .env isn't being loaded at all, not that this feature doesn't work.
If I run NODE_ENV=src yarn start this works fine.
I could've messed it up when refactoring something although I'd expect our E2E tests to catch that.
I don't believe any of our e2e tests use the .env file, only the feature itself.
Sure @gaearon , no package.json imported:
// src/__tests__/modules/statistiche/statistiche.test.js
import { actions } from 'modules/statistiche/actions/statistiche';
describe('Statistiche actions', () => {
it('refreshAll action', () => {
expect(actions.refreshAll()).toMatchSnapshot();
});
// ...
});
Also the imported file is pretty basic:
// src/modules/statistiche/actions/statistiche.js
const REFRESH_ALL = 'statistiche/REFRESH_ALL';
// ...
function refreshAll() {
return {
type: REFRESH_ALL
};
}
@ingro We'd need a separate reproducing project to look into this. Not clear why.
@Timer I think I might know why it happens. env.js currently depends on paths.js which reads NODE_PATH before env.js could set it. 😄
Thanks @gaearon I'll try to create a repo to show the problem in the afternoon
I agree with @gaearon, that's the reason. First we include paths.js where nodePaths is initialized as [] and then read .env files.
@gaearon this is karma for #2034 😆
Lol. I guess we should just duplicate that resolving code in env.js. Since the normal "path resolving" can't apply until we know NODE_PATH.
@ingro BTW I fixed my tests with cross-env so it looks like that now cross-env NODE_PATH=./ react-scripts test --env=jsdom. Ugly hack, but works for now. Waiting for fix :)
in webpack config:
const getClientEnvironment = require('./env');
const paths = require('./paths');
the problem that paths.js will be cached when you include it in the second time. As solution we can remove cache after first reading in env.js
const paths = require('./paths');
delete require.cache[require.resolve('./paths')];
at least works for me.
Thanks for the suggestion @RIP21 but still not working for me, I was already running tests like this:
"scripts": {
"test:cra": "react-scripts test --env=jsdom",
"test": "cross-env NODE_PATH=./src npm run test:cra"
}