You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
Looks like it still not working. In the https://github.com/CodinCat/cra-env-issue exist one App.test.js that testing only App.js, not index.js that contains issue. I have forked this repo into https://github.com/eddig/cra-env-issue.git, update react-scripts to 1.0.2 and fixed the foo/bar component requiring. Now error appears in App.js.
I think the problem with NODE_PATH appears because dotenv not override environment variables, that is already set (in my environment it is /usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript).
Please don't override already set variables. We set these on our Jenkins jobs and allow for each job to set up its own environment whilst keeping a .env file in the repo for development.
I never got cross-env working, nor any combination of using NODE_PATH. Instead, I put a package.json instead of my src folder that had the contents { "name": "src" }. This finally got it working.
@yarbshk that requires ejecting. However I agree that using an "@" symbol is nice, and would put the tool on par with Vue; helping users coming from there.
@tamsanh I really like your approach because I really don't like having to define environment variables everywhere. But I hit an error after defining the package.json as you did, where src/Login/index.js couldnt be resolved when running yarn start. When running eslint, it seemed to assume that every import statement in any file under src was also a package and so threw a ton of errors. Any other details you can provide about how you got your solution working?
However I agree that using an "@" symbol is nice, and would put the tool on par with Vue; helping users coming from there.
If this is valuable why don’t you file an issue for this? 🙂 Maintainers almost never read closed issues because there is too much noise. If feature requests happen in closed issues we won’t see your feature requests.
I’ll lock this issue because it is going off-topic. You are welcome to file more specific issues with your problems and suggestions, but using an old bug for a completely unrelated discussion just serves to confuse future readers coming here from Google etc.
Activity
ingro commentedon May 19, 2017
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 to1.0.0:I've got a problem with paths in tests by the way but I don't know if that's related to
NODE_PATHor to the updated version ofjest, I will report as soon as I could find where exactly the problem is.will-stone commentedon May 19, 2017
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 founderrors.gaearon commentedon May 19, 2017
Interesting. Anyone looked yet into why this happens?
RIP21 commentedon May 19, 2017
Same for me. Have
NODE_PATH=./in .env and imports likesrc/componentsnot working anymore.gaearon commentedon May 19, 2017
Thanks for confirming. Would be good if somebody could take a peek in the source into why. I'd expect it to work.
Timer commentedon May 19, 2017
Seems unrelated to
ModuleScopePlugin. Will investigate more.ingro commentedon May 19, 2017
As stated above I have only problems with tests, moving the
__tests__folder from the root tosrc(since now only tests insidesrcwill run) returns me this weird error (my tests also relies onNODE_PATH):If I change imports in tests to be relative the error disappear.
This doesn't seem related to
jestbecause I created a small repo to try to reproduce this error withoutcrabut it's working fine there.gaearon commentedon May 19, 2017
Can you show the test code? Does it import
package.json?Timer commentedon May 19, 2017
Looks like
.envisn't being loaded at all, not that this feature doesn't work.If I run
NODE_ENV=src yarn startthis works fine.gaearon commentedon May 19, 2017
I could've messed it up when refactoring something although I'd expect our E2E tests to catch that.
Timer commentedon May 19, 2017
I don't believe any of our e2e tests use the
.envfile, only the feature itself.ingro commentedon May 19, 2017
Sure @gaearon , no
package.jsonimported:Also the imported file is pretty basic:
gaearon commentedon May 19, 2017
@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.jscurrently depends onpaths.jswhich readsNODE_PATHbeforeenv.jscould set it. 😄ingro commentedon May 19, 2017
Thanks @gaearon I'll try to create a repo to show the problem in the afternoon
15 remaining items
gaearon commentedon May 19, 2017
react-scripts@1.0.1should be out with a fix for both issues.Can you please verify it helps?
ingro commentedon May 19, 2017
Thanks @gaearon , now my tests are running again :D
eddig commentedon May 21, 2017
Looks like it still not working. In the https://github.com/CodinCat/cra-env-issue exist one
App.test.jsthat testing onlyApp.js, notindex.jsthat contains issue. I have forked this repo into https://github.com/eddig/cra-env-issue.git, update react-scripts to 1.0.2 and fixed thefoo/barcomponent requiring. Now error appears inApp.js.I think the problem with
NODE_PATHappears becausedotenvnot override environment variables, that is already set (in my environment it is/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript).Environment
node --versionv6.10.3
yarn --version0.24.5
ApacheEx commentedon May 21, 2017
@eddig,
I can agree, it won't work if
NODE_PATHis set in your system.I found this: https://github.com/motdotla/dotenv#what-happens-to-environment-variables-that-were-already-set, so
dotenvvariables are skipped in your case.I'd like to ask @gaearon
should we override environment variables that were already set?
will-stone commentedon May 21, 2017
Please don't override already set variables. We set these on our Jenkins jobs and allow for each job to set up its own environment whilst keeping a .env file in the repo for development.
ApacheEx commentedon May 21, 2017
@will-stone, I agree but it can be an option for file
.env.overridegaearon commentedon May 21, 2017
I think in this case the best way to do it is to put
cross-env NODE_PATH=srcbefore the scripts. As I said before this is a bit hacky anyway.RIP21 commentedon Sep 5, 2017
@FezVrasta do you use react-app-rewired?
If so, it's not a problem of CRA. Cause it's known issue of it.
tamsanh commentedon Dec 28, 2017
I never got
cross-envworking, nor any combination of usingNODE_PATH. Instead, I put apackage.jsoninstead of mysrcfolder that had the contents{ "name": "src" }. This finally got it working.RIP21 commentedon Dec 28, 2017
@tamsanh lol. Interesting hack tho :D
Dunno, since NODE_PATH=./src in cross-env or .env works for me just fine.
yarbshk commentedon Jan 17, 2018
I go in the following way (
scripts/webpack.config.dev.js):will-stone commentedon Jan 18, 2018
@yarbshk that requires ejecting. However I agree that using an "@" symbol is nice, and would put the tool on par with Vue; helping users coming from there.
asadakbar commentedon Jan 23, 2018
@tamsanh I really like your approach because I really don't like having to define environment variables everywhere. But I hit an error after defining the package.json as you did, where src/Login/index.js couldnt be resolved when running yarn start. When running eslint, it seemed to assume that every import statement in any file under src was also a package and so threw a ton of errors. Any other details you can provide about how you got your solution working?
gaearon commentedon Jan 23, 2018
If this is valuable why don’t you file an issue for this? 🙂 Maintainers almost never read closed issues because there is too much noise. If feature requests happen in closed issues we won’t see your feature requests.
I’ll lock this issue because it is going off-topic. You are welcome to file more specific issues with your problems and suggestions, but using an old bug for a completely unrelated discussion just serves to confuse future readers coming here from Google etc.
Thanks!