Solvedangular cli angular-cli doesn't work when node_modules is located in a parent directory

Please provide us with the following information:


Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?)
Alpine linux (inside Docker container)


Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here:

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 (/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.

  1. ng new ngapp
  2. cd ngapp
  3. mv node_modules ..
  4. 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.

43 Answers

✔️Accepted Answer

Any news about that?

My use case is the following:

I'm building an app with backend and frontend both merged into a monorepo.

I do have the following structure:

| package.json
| node_modules/
| backend/
| frontend/
  | .angular-cli.json

I'd love to hear from you if you've managed to solved that in a clean way :)
(avoiding symlinks or moving .angular-cli.json to top folder)


Other Answers:

@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.

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:

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".

@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)

I'm working inside a Electron-Angular4.x project and to resolve this what I did was move the angular-cli.json in the same dir as node_modules and just changed the root path in the angular-cli.json

This fixed the errors I was getting related to everything angular-cli

screen shot 2017-08-31 at 6 31 53 pm

More Issues: