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
{{ message }}
Repository navigation
angular-cli doesn't work when node_modules is located in a parent directory #3864
Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?)
Alpine linux (inside Docker container)
Versions.
Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here:
1.0.0-beta.24
but running ng --version gives:
Cannot find module '/opt/app/ngapp/node_modules/@angular/common/package.json'
Error: Cannot find module '/opt/app/ngapp/node_modules/@angular/common/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)
at Class.getVersion (/opt/app/node_modules/angular-cli/commands/version.js:67:25)
at /opt/app/node_modules/angular-cli/commands/version.js:63:67
at Array.forEach (native)
at Class.getDependencyVersions (/opt/app/node_modules/angular-cli/commands/version.js:63:14)
at /opt/app/node_modules/angular-cli/commands/version.js:41:58
at Array.forEach (native)
at Class.run (/opt/app/node_modules/angular-cli/commands/version.js:40:19)
at Class.<anonymous> (/opt/app/node_modules/angular-cli/ember-cli/lib/models/command.js:152:17)
at process._tickCallback (internal/process/next_tick.js:103:7)
Repro steps.
Was this an app that wasn't created using the CLI? What change did you do on your code? etc.
ng new ngapp
cd ngapp
mv node_modules ..
ng build
The log given by the failure.
Normally this include a stack trace and some more information.
You seem to not be dependending on "@angular/core". This is an error.
Mention any other details that might be useful.
I want to use angular CLI inside a docker container using docker-compose. I cannot have the node_modules folder inside the source code and a common pattern to solve this issue is to move the node_modules folder to the parent directory. Node will correctly recognise dependencies, but for some reason angular CLI is failing to find the @angular/core package.
If I run node and then do require('@angular/core') the package is correctly loaded.
@wooltar, I appreciate your extensive response, but as I said in the my post I cannot put the node_modules in the source directory as Docker compose will sync that with my host system. Other than the permissions issues that Docker will copy the node_modules with into the host, it means that I can't run, e.g. Jamine in the host as any binaries will be compiled for the version of Alpine in the container and not Ubuntu which I use for development.
Reacted by Niels Van Vliet, Benjamin Longearet, VinayBinny, jjw99, Sasha Rudan, Oscar Lodriguez, rujorgensen, Tim Bendt and Miles Stanfield
You could try exporting your node instance's node_modules PATH variable as ../node_modules. The command for this will vary depending on your host operating system.
@gelliott181 node can find the node_modules itself, it;s something in angular-cli that explicitly tries to look in node_modules relative to the current directory (i.e. not using node's mechanism)
Reacted by Todd, Tommy Seus, Ben Abbenanti, Sune Brøndum Wøller, Christian Bundy, David Enke and rujorgensen
I have a slightly different use case for this same feature, not specific to Docker, and I encounter the same error for the same reason. I have an Angular 2 app living inside an outer npm-based app (the outer app having the package.json). I would like to be able to use the outer node_modules for both apps, without copying anything. Running ng build from the Angular 2 app directory yields the same error:
You seem to not be dependending on "@angular/core". This is an error.
In my case I can work around this with a symlink, but that adds complexity and introduces portability concerns.
Reacted by Elliot, Kyle Cordes, Jordan, Tally Barak, ochstobi and rujorgensen
If the module identifier passed to require() is not a core module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node will not append node_modules to a path already ending in node_modules.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.
I think it can be worked around with some bash hackery to add symlinks - but that's ugly, and the current behavior violates the Node module resolution "spec".
Reacted by Pawel Kozlowski, JulienBourgain, Benjamin Longearet, Ben Abbenanti, Sune Brøndum Wøller, John, Sasha Rudan, Birk and Tim Bendt
@kylecordes I think I see what's happening, we added full node resolution to webpack but still check direct dependencies when running ng commands. Thanks for testing this, I'll reopen.
For the record, if you would like to continue using 6.1.0 rather than reverting back, as a temporary solution, just copy your package.json to your frontend and backend folders, you'll have 2 copies of node_module but that should keep it running fine until this is solved.
The issue is happening only because somehow it's not applying the node module resolution to search in the parent folder if the node_modules isn't found in the current folder, but if it finds the node_modules in the current folder, everything should be running fine.
@NightwareSystems that won't work in our case (which is the docker-compose mounted volume problem where we use a symlink inside the image to point node_modules to a folder one level up in the image to prevent them from being shared with the host). I have no idea what features we're missing out on, but we've locked our version of this package to 6.0.8 until the issue is resolved.
I go back to my initial question, though, which is why is this not just using node's resolution all the way through? I'm sure there is some reason the devs decided they needed to do explicit checks when running ng commands but I cannot for the life me see what those reasons might be... hoping we can get an explanation on that point because it's a little confusing (node's resolution should suffice in all cases... if you don't have things installed they don't work, if you do... they do)
@filipesilva ^^^ (at-mentioning you just in case the closed status of this issue has prevented you from seeing the recent comments). This issue has regressed.
@filipesilva Yeah I think I can do that, although will not be able to right away (deadlines and such). Basically though, if you have your node_modules folder sitting in a parent dir and symlinked in, this fails. Hopefully will be able to make a clear repro setup for you later tonight/tomorrow.
I have node_modules for my angular app in the app itself. If I install all my external deps and build everything is ok. But if I then change my tsconfig.app.json file so it adds a path reference to my shared code, then I get errors resolving 'fs' in aws-sdk (which is pulled in through aws-amplify which I am using) in node_modules.
If I remove the paths section and bring my local dependencies from my repo into the angular project then everything works fine. It's strange that adding the paths would have this knock on effect of causing the aws-sdk to cause a compilation error.
Why isn't this reopen? This problem persists and as @kylecordes pointed out the current behavior violates the module resolution rules of node.js and breaks every sane MonoRepo structure (like plenty of people here are using). I would like to have my Backend and Frontend dependencies separated and only a small root-node_module.
Neither yarn workspaces nor relative PATH inside the package.json is working because suddenly rxjs Objects are not the same anymore (Observable is not assignable to Observable) and angular can't build anymore.
Activity
erscoder commentedon Jan 8, 2017
You have to put the node_module directory in the angular directory. (
then config angular-cli.json:
`{
"project": {
"version": "1.0.0-beta.24",
"name": "prueba"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"test": "test.ts",
"tsconfig": "tsconfig.json",
"prefix": "app",
"mobile": false,
"styles": [
],

"addons": [
"node_modules/font-awesome/fonts/*.+(otf|eot|svg|ttf|woff|woff2)"
],
"packages": [],
"e2e": {
"protractor": {
"config": "config/protractor.conf.js"
}
},
"test": {
"karma": {
"config": "config/karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"prefixInterfaces": false,
"inline": {
"style": false,
"template": false
},
"spec": {
"class": false,
"component": true,
"directive": true,
"module": false,
"pipe": true,
"service": true
}
}
}`
here my project structure
i hope this help you.
euangoddard commentedon Jan 8, 2017
@wooltar, I appreciate your extensive response, but as I said in the my post I cannot put the
node_modulesin the source directory as Docker compose will sync that with my host system. Other than the permissions issues that Docker will copy thenode_moduleswith into the host, it means that I can't run, e.g. Jamine in the host as any binaries will be compiled for the version of Alpine in the container and not Ubuntu which I use for development.celliott181 commentedon Jan 9, 2017
You could try exporting your node instance's
node_modulesPATH variable as../node_modules. The command for this will vary depending on your host operating system.euangoddard commentedon Jan 16, 2017
@gelliott181 node can find the
node_modulesitself, it;s something in angular-cli that explicitly tries to look innode_modulesrelative to the current directory (i.e. not using node's mechanism)filipesilva commentedon Jan 24, 2017
This is the source of that message:
https://github.com/angular/angular-cli/blob/master/packages/angular-cli/upgrade/version.ts#L100
Maybe it could be done in a better way that supports your usecase.
wcainboundary commentedon Feb 16, 2017
I have a slightly different use case for this same feature, not specific to Docker, and I encounter the same error for the same reason. I have an Angular 2 app living inside an outer npm-based app (the outer app having the package.json). I would like to be able to use the outer node_modules for both apps, without copying anything. Running
ng buildfrom the Angular 2 app directory yields the same error:In my case I can work around this with a symlink, but that adds complexity and introduces portability concerns.
kylecordes commentedon Apr 6, 2017
I've hit this same problem, the "You seem to not be depending".
This issue might be more than a "nice to have" - the current behavior breaks the Node module resolution rules:
https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
I think it can be worked around with some bash hackery to add symlinks - but that's ugly, and the current behavior violates the Node module resolution "spec".
filipesilva commentedon May 22, 2017
Fixed by #6276.
kylecordes commentedon May 22, 2017
This problem appears to still be present in 1.0.4:
In the transcript above, I have these files up a couple of levels in node_modules; node module resolution should find them.
Should this issue be reopened? Should I enter a new one instead?
filipesilva commentedon May 22, 2017
@kylecordes I think I see what's happening, we added full node resolution to webpack but still check direct dependencies when running
ngcommands. Thanks for testing this, I'll reopen.21 remaining items
ryedin commentedon Jul 27, 2018
Why would this not just use node's resolution? These checks seem quite artificial. Also... 6.1.0 breaks our setup (same symlinked/docker thing)
htowaileb commentedon Jul 28, 2018
Getting the same error as well after upgrading to 6.1.0.
You seem to not be depending on "@angular/core" and/or "rxjs". This is an error.I got my frontend and backend folders, and node_modules is setting in a parent folder.
kristy-mj commentedon Jul 31, 2018
+1 ^ Looks like this issue has regressed, please reopen
htowaileb commentedon Jul 31, 2018
For the record, if you would like to continue using
6.1.0rather than reverting back, as a temporary solution, just copy yourpackage.jsonto yourfrontendandbackendfolders, you'll have 2 copies ofnode_modulebut that should keep it running fine until this is solved.The issue is happening only because somehow it's not applying the node module resolution to search in the parent folder if the
node_modulesisn't found in the current folder, but if it finds thenode_modulesin the current folder, everything should be running fine.ryedin commentedon Jul 31, 2018
@NightwareSystems that won't work in our case (which is the docker-compose mounted volume problem where we use a symlink inside the image to point node_modules to a folder one level up in the image to prevent them from being shared with the host). I have no idea what features we're missing out on, but we've locked our version of this package to 6.0.8 until the issue is resolved.
I go back to my initial question, though, which is why is this not just using node's resolution all the way through? I'm sure there is some reason the devs decided they needed to do explicit checks when running
ngcommands but I cannot for the life me see what those reasons might be... hoping we can get an explanation on that point because it's a little confusing (node's resolution should suffice in all cases... if you don't have things installed they don't work, if you do... they do)ryedin commentedon Jul 31, 2018
@filipesilva ^^^ (at-mentioning you just in case the closed status of this issue has prevented you from seeing the recent comments). This issue has regressed.
filipesilva commentedon Jul 31, 2018
@ryedin can you open a new issue with a reproduction that we can follow to see this problem please?
ryedin commentedon Jul 31, 2018
@filipesilva Yeah I think I can do that, although will not be able to right away (deadlines and such). Basically though, if you have your node_modules folder sitting in a parent dir and symlinked in, this fails. Hopefully will be able to make a clear repro setup for you later tonight/tomorrow.
clydin commentedon Jul 31, 2018
These two PRs should help to mitigate the issue: #11691 (already merged) & #11725
ryedin commentedon Aug 1, 2018
@filipesilva @clydin do you still need a repro setup for this issue? (asking because of @clydin 's response above indicating potential incoming fixes)
johneast commentedon Oct 4, 2018
I have a similar issue where I have backend code and an Ionic 4/Angular 6 app in a mono repo with some shared code outside of the angular project
I have node_modules for my angular app in the app itself. If I install all my external deps and build everything is ok. But if I then change my tsconfig.app.json file so it adds a path reference to my shared code, then I get errors resolving 'fs' in aws-sdk (which is pulled in through aws-amplify which I am using) in node_modules.
My tsconfig.app.json looks like this
If I remove the
pathssection and bring my local dependencies from my repo into the angular project then everything works fine. It's strange that adding the paths would have this knock on effect of causing the aws-sdk to cause a compilation error.bb1 commentedon Apr 9, 2019
Why isn't this reopen? This problem persists and as @kylecordes pointed out the current behavior violates the module resolution rules of node.js and breaks every sane MonoRepo structure (like plenty of people here are using). I would like to have my Backend and Frontend dependencies separated and only a small root-node_module.
Neither yarn workspaces nor relative PATH inside the package.json is working because suddenly rxjs Objects are not the same anymore (Observable is not assignable to Observable) and angular can't build anymore.
WalterSheng commentedon Apr 15, 2019
@bb1 It's fixed by #12325. Just upgrade CLI to v7.0.
angular-automatic-lock-bot commentedon Sep 9, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.