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

Please provide us with the following information:

OS?

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.

  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.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 26
  • Comments: 42 (8 by maintainers)

Commits related to this issue

Most upvoted comments

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)

Thanks

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

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

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

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

I recently updated @angular/cli in my project to 6.1.0 and now I am getting the error:

You seem to not be depending on "@angular/core" and/or "rxjs". This is an error.

My node_modules is not in the root of my CLI project, but one folder up and symlinked, so node_modules in the CLI project’s root points at …/node_modules.

Prior to 6.1.0, this worked fine. But 6.1.0 now seems to check if the node_modules directory is inside the CLI project root and this fails on my setup.

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.

@daviddiefenderfer Your solution worked for me (in a .net project), but I do get a side effect on ng build:

WARNING in ./ClientApp/node_modules/@angular/compiler/@angular/compiler.es5.js (Emitted value instead of an instance of Error) Cannot find source file ‘compiler.es5.ts’: Error: Can’t resolve ‘./compiler.es5.ts’

Don’t suppose you’ve any idea’s on working around that?

(Part of such a test could also replace some of the node_module/ZZZ directories with symlinks, to further tease out symlink issues and demonstrate the tooling tolerates symlinks.)

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”: [

    "../node_modules/font-awesome/css/font-awesome.css",
    "../node_modules/primeng/resources/themes/cupertino/theme.css",
    "../node_modules/primeng/resources/primeng.min.css"
  ],
  "scripts": [],
  "environments": {
    "source": "environments/environment.ts",
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
  }
}

], “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 captura

i hope this help you.

Same problem as @maxime1992 - any solution yet?

Funnily enough, the problem that @maxime1992 is the same one that I have and I also have an identical project structure.

Both frontend and backend dependencies being defined in the same package.json file, but as angular-cli belongs in the frontend, it’s placed in that sub-directory.

is there any way to access node_modules outside of angular project,I mean ,instead of accessing relative path can we access absolute path for my development activity.

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

This problem appears to still be present in 1.0.4:

Kyles-MBP:agent kcordes$ ng build
You seem to not be depending on "@angular/core". This is an error.

Kyles-MBP:agent kcordes$ ng --version
    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.0.4
node: 7.10.0
os: darwin x64
@angular/common: error
@angular/compiler: error
@angular/core: error
@angular/forms: error
@angular/http: error
@angular/platform-browser: error
@angular/platform-browser-dynamic: error
@angular/router: error
@angular/cli: error
@angular/compiler-cli: error

Kyles-MBP:agent kcordes$ ls ../../node_modules/\@angular/core/
@angular		core.d.ts		public_api.d.ts		testing.d.ts
README.md		core.metadata.json	src			testing.metadata.json
bundles			package.json		testing

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?

You can take a look at nrwl/nx. It should provide an example of what you all need. At Elium, we are running a stack of 2 frontend apps, 2 backend apps and 4 libraries at the moment in a monorepo. We’re using Yarn workspaces so each app / lib can have its own package.json. The .angular-cli.json config file is located in the root, and it makes sense to us since it declares apps and libs.

@nickdnk couldn’t find anything yet on my side.

@filipesilva could you reopen? (should I open a new issue? It really seems to be on this one but wondering…)

@filipesilva How about a test case (or N of them) proving the whole thing works with empty local node_modules, and all the deps scattered among 1, 2, 3 levels “up”? Is there an “E2E” part of the test suite where such a thing could fit?