Repository navigation
Can't use NODE_PATH with two directories #3676
Copy link
Copy link
Closed
Labels
Description
Is this a bug report?
Yes
Can you also reproduce the problem with npm 4.x?
Did not try
Which terms did you search for in User Guide?
NODE_PATH, indexOf, undefined
Environment
node -v: v8.7.0npm -v: 5.6.0yarn --version(if you use Yarn):npm ls react-scripts(if you haven’t ejected):
Then, specify:
- Operating system: MacOS
- Browser and version (if relevant): N/A
Steps to Reproduce
(Write your steps here:)
- Create a
.envfile in the root folder. Add NODE_PATH to it containing two directories:NODE_PATH=src:../shared - Run
npm start. You will get the following error:
/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/react-dev-utils/ModuleScopePlugin.js:28
request.descriptionFileRoot.indexOf('/node_modules/') !== -1 ||
^
TypeError: Cannot read property 'indexOf' of undefined
at Resolver.resolver.plugin (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/react-dev-utils/ModuleScopePlugin.js:28:37)
at Resolver.applyPluginsAsyncSeriesBailResult1 (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/tapable/lib/Tapable.js:256:13)
at runNormal (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:130:20)
at Resolver.doResolve (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:116:3)
at Resolver.<anonymous> (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/TryNextPlugin.js:16:12)
at Resolver.applyPluginsAsyncSeriesBailResult1 (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/tapable/lib/Tapable.js:256:13)
at runNormal (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:130:20)
at Resolver.doResolve (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:116:3)
at Resolver.<anonymous> (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/FileKindPlugin.js:17:12)
at Resolver.applyPluginsAsyncSeriesBailResult1 (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/tapable/lib/Tapable.js:256:13)
at runNormal (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:130:20)
at Resolver.doResolve (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:116:3)
at Resolver.<anonymous> (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/NextPlugin.js:14:12)
at Resolver.applyPluginsAsyncSeriesBailResult1 (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/tapable/lib/Tapable.js:256:13)
at runAfter (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:152:20)
at innerCallback (/Users/nbhati/projects/myapp-monorepo/myapp-embeds/node_modules/enhanced-resolve/lib/Resolver.js:146:3)
Expected Behavior
App is able to build correctly using packages from src and ../shared
Actual Behavior
Build crashes with TypeError: Cannot read property 'indexOf' of undefined
Reproducible Demo
None
Reactions are currently unavailable
Activity
miraage commentedon Jan 3, 2018
descriptionFileRootis a directory of file being processed, managed byenhanced-resolvepackage (webpackdependency).Source:
directoryappears to be undefined for some reason.AFAIK standard NodeJS (e.g. not installed by tools like
nvm) provides its ownNODE_PATHenvironment variable so you can not override it using.envfiles. I assume it might cause an issue, meanwhile I recall using customNODE_PATHvia.env(I'm usingnvmwhich does not exposeNODE_PATHenv var) and everything worked as expected.According to issue template, you have ejected your application. Could you please show output of
process.env.NODE_ENVafter loading env vars? E.g. after this line inscripts/starts.jsnareshbhatia commentedon Jan 3, 2018
Hi @miraage, thanks for the quick response.
process.env.NODE_ENVis outputtingdevelopment. You are correct that I have ejected. Note that NODE_PATH is working with one directory only:NODE_PATH=src. Also note that I am usingnfor installing multiple versions of node.miraage commentedon Jan 3, 2018
Sorry for wrong question. I meant
process.env.NODE_PATH:)I'm not sure if
nexposesNODE_PATHor not.Timer commentedon Jan 3, 2018
We intentionally do not support multiple directories inNODE_PATH. You can modify your ejected setup to allow this if you'd like.miraage commentedon Jan 3, 2018
@Timer I always thought CRA supports multiple NODE_PATH dirs. I clearly recall using it..
Timer commentedon Jan 3, 2018
Oh wow, my memory failed me! My apologies.
I'd heed @miraage's advice and check what the value of
NODE_PATHis; it doesn't get overridden by.env--.envonly provides the default if it's not defined.miraage commentedon Jan 3, 2018
@nareshbhatia are you using symlinks under the
NODE_PATH? I guess it might affect path resolving in some way..nareshbhatia commentedon Jan 3, 2018
Here's the value of
NODE_PATH:I am not using any symlinks. The project is structured as follows:
miraage commentedon Jan 3, 2018
There is also a line inside
config/env.jsafter eject:@nareshbhatia could you please try to write
NODE_PATH=./src:../shared? I assumesrcmight cause an issue here.nareshbhatia commentedon Jan 3, 2018
No change :-(.
NODE_PATH=./src:../sharedproduces the same error.If it helps, I can try to create a minimal app that reproduces this issue.
miraage commentedon Jan 3, 2018
Let's try.
nareshbhatia commentedon Jan 3, 2018
Ok, on it!
4 remaining items
nareshbhatia commentedon Jan 5, 2018
@miraage, I completely removed the
ModuleScopePluginfromwebpack.config.dev.js, but the error still persists.Unfortunately, I don't have more time to work on this issue. However, I think it is an important one to solve. I see that number of other people are concerned about it too (e.g. #1492). I hope that the CRA team will give it the priority that it deserves. Thanks again for your help.
miraage commentedon Jan 5, 2018
@nareshbhatia @Timer
I did a test. Fresh ejected CRA app.
Added
sharedpath toconfig.paths.js:Commented
ModuleScopePluginsince it does not support multiple src directories.Added
sharedtoincludesection in eslint/babel loaders.Created a
SharedCompcomponent insharedfolder:Added
NODE_PATH:Updated
src/App.js:Works like a charm: screen
Don't forget to update both dev/prod webpack configuration files.
nareshbhatia commentedon Jan 5, 2018
@miraage, thanks for the help.
Your solution works because
srcandsharedare peers:I am looking for more separation so that two CRA apps can access shared components:
In this case, you will have to set
NODE_PATHto../shared:./srcandappSharedto../shared. The moment you do this, CRA stops recognizing shared files as being JSX and starts throwing syntax errors. This is the use case I was trying to solve in my repo.miraage commentedon Jan 6, 2018
@nareshbhatia you must mimic all actions I've provided. Then you'll have no problems at all.
nareshbhatia commentedon Jan 6, 2018
@miraage, I should have clarified - I tried all actions suggested by you both ways - once with
srcandsharedbeing peers and then withsharedat../shared. Former worked, latter didn't. There is something about going up the tree that is tripping up the build.razum2um commentedon Sep 24, 2018
I'm about to extract some common code between 2 CRA's in a monorepo, and have the same issue:
NODE_PATH=../shared:srcin both CRAs makes eslint work, but webpack needs moreincludes forbabel-loaderI wonder, why CRA supports multiple NODE_PATH, but webpack/babel-loader (at least) processes only
appSrc, I think more logical would be similar approach: either 1 root or many in all places.IMO described monorepo structure feels very natural, but instead, the current approach forces to have a real private npm package (preprocessed to es5 ofc) => which forces either setup a private registry or check in preprocessed es5 code
@Timer, btw, could you tell about #2189, because it's not self-explanatory, why did you do it
@miraage thank you for your efforts to show detailed way after eject in #3676 (comment), still I'd rather not, and given existence of "Alternatives to Ejecting" in readme, others don't want to eject as well ;)
p.s. I have no clear vision how this could be solved, but I can imagine some tools discovering their's configuration recursively when building sub-projects, wdyt about this?
p.p.s just in case: eslint is working after putting into
.eslintrc.js:Timer commentedon Sep 24, 2018
We don't compile source code outside of
src/so there's no sense in importing it, it's a no-operation.We can't compile all source code because certain files need certain transforms.
razum2um commentedon Sep 24, 2018
@Timer thanks for a fast reply, still, why support multiple
NODE_PATHthen? I see it misleading (worse with easy success with eslint): maybe warn that other entries should be either es5 or "please eject then"?nareshbhatia commentedon Sep 24, 2018
@razum2um, I finally solved my code sharing issue using yarn-workspaces. You can read about my solution here: Sharing UI Components with Lerna and Yarn Workspaces.
Timer commentedon Sep 24, 2018
NODE_PATHexists for absolute imports, some people will dosrc/components/andsrc/containers/and make sure there are unique namesdarksh3ll commentedon Oct 3, 2018
The react-scripts package provided by Create React App requires a dependency:
"eslint": "5.6.0"
stale commentedon Nov 2, 2018
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 7 days if no further activity occurs.