question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

14reactions
maxime1992commented, Mar 4, 2018

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

10reactions
kylecordescommented, 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

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set node_modules in a parent folder of project to ...
I'd like use a Angular framework like a template and use to make a lot contents, but for this, a need use a...
Read more >
npm-link
First, npm link in a package folder with no arguments will create a symlink in the global folder {prefix}/lib/node_modules/<package> that links to the...
Read more >
TypeScript configuration - Angular
This page covers some aspects of TypeScript configuration and the TypeScript environment that are important to Angular developers, including details about ...
Read more >
The magic behind npm link - Medium
Look for the module in the ./node_modules directory. If it is not there, recursively search in the parent directories' ./node_modules until either the...
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
js starts at the directory of the current module, and adds /node_modules , and attempts to load the module from that location. Node.js...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found