cli icon indicating copy to clipboard operation
cli copied to clipboard

npm v7 does not install linked packages dependencies

Open garciaalvaro opened this issue 5 years ago • 79 comments

Current Behavior:

npm v7 does not install linked local packages dependencies.

Expected Behavior:

In npm v6 the dependencies of a local linked package are installed. This means that if app has my-local-pkg as a dependency, running npm install generates app/node_modules folder and also my-local-pkg/node_modules (with its dependencies). In npm v7, however, only app/node_modules is generated.

Steps To Reproduce:

I created the following repository to illustrate it. The README.md has the instructions on how to reproduce it. Simplified, the steps are:

  1. Add the following folder structure: ./app and ./my-local-pkg, each with their own package.json
  2. Include this in the ./app/package.json:
{
  "name": "app",
  "dependencies": {
    "my-local-pkg": "file:../my-local-pkg",
    "prettier": "^2.2.1"
  }
}
  1. Include this in the ./my-local-pkg/package.json:
{
  "name": "my-local-pkg",
  "dependencies": {
    "typescript": "^4.1.3"
  }
}
  1. Go to ./app and run npm install. npm version 6 produces:
node_modules
	.bin
	my-local-pkg
		node_modules
			typescript
	prettier

while npm version 7 produces:

node_modules
	.bin
	my-local-pkg
	prettier

Compare the two outputs, npm v7 does not install my-local-pkg dependencies, there is no ./my-local-pkg/node_modules folder generated.

Environment:

npm v7.1.2

garciaalvaro avatar Dec 12 '20 11:12 garciaalvaro

Same behavior with [email protected]

raphinesse avatar Dec 16 '20 15:12 raphinesse

I'd also add that for me the expected behavior for local installs npm install ../some-package is to result in the same package-lock.json as installs from the registry with the only difference being the "resolved" field of local install pointing to ../some-package instead of to the registry (and other related metadata like "link": true).

This means that the dependencies of the locally installed package should end up in the top level of node_modules unless there is a version conflict. I believe that v6 puts all of the local install package's dependencies in its node_modules and not in the top level node_modules (hopefully v7 will change this).

Having the install behavior be consistent will make it easier to develop npm packages by being able to install the package locally in development to test it out and know that it will act the same as installing it from the registry when it's published.

For example, with Rollpkg:

git clone [email protected]:rafgraph/rollpkg.git
mkdir test && cd test
npm init --yes
# compare package-lock.json after running the two commands
npm i --save-dev ../rollpkg
# vs
npm i --save-dev rollpkg

Note that Rollpkg, a zero config build tool to create packages with Rollup and TypeScript, has Jest and ESLint as dependencies (and provides default configs for these), but let's the user call jest and eslint instead of wrapping those commands, which relies on those bins being in the top level of node_modules. The advantage of not wrapping the jest and eslint commands is that if the user needs to use a specific version of Jest or ESlint all they have to do is install that version and it will override the version provided by Rollpkg.

rafgraph avatar Dec 20 '20 15:12 rafgraph

@rafgraph for that, jest and eslint should be peer deps - and linking with peer deps means you have to link the peer deps also.

ljharb avatar Dec 20 '20 15:12 ljharb

Not exactly peer deps, it's a default with optional override. I know this isn't a standard use case, but regardless a consistent package-lock.json would be nice to have.

rafgraph avatar Dec 20 '20 15:12 rafgraph

@garciaalvaro if the path is set within the root directory we will resolve the dependencies in v7 but, if as you've shown, the path is outside the fs root then we treat it like a linked dep and won't resolve those deps. We understand that this is a change from v6.

darcyclarke avatar Jan 15 '21 19:01 darcyclarke

@darcyclarke Thank you for the information. If I understood correctly, the behaviour is intended. Is there some way to keep/emulate the previous behaviour and install those dependencies too?

garciaalvaro avatar Jan 22 '21 18:01 garciaalvaro

@garciaalvaro I'm going to bring this up at our next Open RFC call & see what the consensus is in terms of supporting the old behaivour; I forgot the exact reasoning behind the change but I can imagine that it would have reduced some complexity & potentially cleaned up a number of issues/edgecases. I'll circle back here after the call on Wednesday.

darcyclarke avatar Jan 26 '21 07:01 darcyclarke

We do have the same problem im @sulu where the package.json can not be the in the parent directory as it would block the package.json which developers could maybe create to build there own application/website css/js.

@darcyclarke I did read in the meeting doc that:

we have options here in terms of making this more configurable

Is this a suggestion to add options or are here already some options to get the old behaviour back?

alexander-schranz avatar Feb 09 '21 11:02 alexander-schranz

Not sure if this is the same issue or if I should open a new one—please let me know. I cannot npm link typescript in v7, but can in 6:

v7 (fail):

example % nvm ls --no-alias
       v14.15.5
->      v15.8.0

example % node --version
v15.8.0

example % npm --version
7.5.2

example % npm -g ls
/Users/jesse/.nvm/versions/node/v15.8.0/lib
├── @typescript-eslint/[email protected]
├── @typescript-eslint/[email protected]
├── [email protected]
├── [email protected]
└── [email protected]


example % ls -a
.	..

example % npm link typescript
npm ERR! code 127
npm ERR! path /Users/jesse/.nvm/versions/node/v15.8.0/lib/node_modules/typescript
npm ERR! command failed
npm ERR! command sh -c gulp build-eslint-rules
npm ERR! sh: gulp: command not found

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/jesse/.npm/_logs/2021-02-10T01_33_13_597Z-debug.log
debug.log
example % cat /Users/jesse/.npm/_logs/2021-02-10T01_33_13_597Z-debug.log
0 verbose cli [
0 verbose cli   '/Users/jesse/.nvm/versions/node/v15.8.0/bin/node',
0 verbose cli   '/Users/jesse/.nvm/versions/node/v15.8.0/bin/npm',
0 verbose cli   'link',
0 verbose cli   'typescript'
0 verbose cli ]
1 info using [email protected]
2 info using [email protected]
3 timing config:load:defaults Completed in 1ms
4 timing config:load:file:/Users/jesse/.nvm/versions/node/v15.8.0/lib/node_modules/npm/npmrc Completed in 0ms
5 timing config:load:builtin Completed in 1ms
6 timing config:load:cli Completed in 1ms
7 timing config:load:env Completed in 0ms
8 timing config:load:file:/Users/jesse/example/.npmrc Completed in 0ms
9 timing config:load:project Completed in 1ms
10 timing config:load:file:/Users/jesse/.npmrc Completed in 2ms
11 timing config:load:user Completed in 2ms
12 timing config:load:file:/Users/jesse/.nvm/versions/node/v15.8.0/etc/npmrc Completed in 0ms
13 timing config:load:global Completed in 0ms
14 timing config:load:cafile Completed in 0ms
15 timing config:load:validate Completed in 1ms
16 timing config:load:setUserAgent Completed in 1ms
17 timing config:load:setEnvs Completed in 0ms
18 timing config:load Completed in 8ms
19 verbose npm-session d489e3dfc3b55581
20 timing npm:load Completed in 15ms
21 timing arborist:ctor Completed in 1ms
22 timing arborist:ctor Completed in 0ms
23 timing arborist:ctor Completed in 0ms
24 timing idealTree:init Completed in 5ms
25 timing idealTree:userRequests Completed in 2ms
26 silly idealTree buildDeps
27 silly placeDep ROOT [email protected] OK for:  want: file:../.nvm/versions/node/v15.8.0/lib/node_modules/typescript
28 timing idealTree:#root Completed in 4ms
29 timing idealTree:node_modules/typescript Completed in 0ms
30 timing idealTree:buildDeps Completed in 5ms
31 timing idealTree:fixDepFlags Completed in 0ms
32 timing idealTree Completed in 13ms
33 timing reify:loadTrees Completed in 13ms
34 timing reify:diffTrees Completed in 1ms
35 silly reify moves {}
36 timing reify:retireShallow Completed in 1ms
37 timing reify:createSparse Completed in 1ms
38 timing reify:loadBundles Completed in 0ms
39 timing reifyNode:node_modules/typescript Completed in 34ms
40 timing reify:unpack Completed in 34ms
41 timing reify:unretire Completed in 0ms
42 timing build:queue Completed in 0ms
43 timing build:deps Completed in 0ms
44 timing build:queue Completed in 0ms
45 info run [email protected] prepare ../.nvm/versions/node/v15.8.0/lib/node_modules/typescript gulp build-eslint-rules
46 info run [email protected] prepare { code: 127, signal: null }
47 timing reify:rollback:createSparse Completed in 1ms
48 timing reify:rollback:retireShallow Completed in 0ms
49 timing command:link Completed in 89ms
50 verbose stack Error: command failed
50 verbose stack     at ChildProcess.<anonymous> (/Users/jesse/.nvm/versions/node/v15.8.0/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/index.js:64:27)
50 verbose stack     at ChildProcess.emit (node:events:378:20)
50 verbose stack     at maybeClose (node:internal/child_process:1067:16)
50 verbose stack     at Socket.<anonymous> (node:internal/child_process:453:11)
50 verbose stack     at Socket.emit (node:events:378:20)
50 verbose stack     at Pipe.<anonymous> (node:net:665:12)
51 verbose pkgid [email protected]
52 verbose cwd /Users/jesse/example
53 verbose Darwin 20.3.0
54 verbose argv "/Users/jesse/.nvm/versions/node/v15.8.0/bin/node" "/Users/jesse/.nvm/versions/node/v15.8.0/bin/npm" "link" "typescript"
55 verbose node v15.8.0
56 verbose npm  v7.5.2
57 error code 127
58 error path /Users/jesse/.nvm/versions/node/v15.8.0/lib/node_modules/typescript
59 error command failed
60 error command sh -c gulp build-eslint-rules
61 error sh: gulp: command not found
62 verbose exit 127

v6 (success):

example % nvm use default
Now using node v14.15.5 (npm v6.14.11)

example % node --version
v14.15.5

example % npm --version
6.14.11

example % npm link typescript
/Users/jesse/example/node_modules/typescript -> /Users/jesse/.nvm/versions/node/v14.15.5/lib/node_modules/typescript

jsejcksn avatar Feb 10 '21 01:02 jsejcksn

I am also running into the same exact issue as @jsejcksn . Is there a workaround here without downgrading back to npm v6?

@jsejcksn how did you move forward with this?

cc: @arjunpatel

munjalpatel avatar Mar 20 '21 01:03 munjalpatel

Is there a workaround here without downgrading back to npm v6?

@jsejcksn how did you move forward with this?

@munjalpatel Not yet. I'm just using npm 6.x until it's fixed.

jsejcksn avatar Mar 20 '21 22:03 jsejcksn

A common use case (and good practice, I believe) is having a configuration packages such as eslint-config-..., jest-config-... etc. They contain eslint, jest and other stuff, which they need to function, as peer dependencies and usually are npm-linked across all my projects. These allows me to avoid cluttering project's package.json with dozens of linting/testing dependencies, that repeat in every project. So, when I create a new project and link my eslint-config- there, I'd expect that their peer dependencies will be installed automatically in my project when I run npm install. I expected npm7 to solve this, but... Even worse is that npm install still deletes all npm-linked packages from node_modules... (as of npm 7.8.0).

cyberbiont avatar Apr 09 '21 22:04 cyberbiont

In npm6 I could npm install ./packages/my-local-folder, which contains a package.json with some extra dependencies, and it would hoist the dependencies to the root level (e.g. the path where npm install was run).

In npm7 it just generates an empty symlink to ../packages/my-local-folder without actually installing anything.

neutraali avatar May 07 '21 15:05 neutraali

same here, some workarround?

albert-asin avatar May 25 '21 12:05 albert-asin

@albert-asin current workaround is using NPM v6 until this issue is fixed.

alexander-schranz avatar May 25 '21 12:05 alexander-schranz

@alexander-schranz thanks yes, rolling back to v6 works again

albert-asin avatar May 26 '21 10:05 albert-asin

I tried today using instead of file: the new "workspaces" feature of npm 7:

+    "workspaces": [
+        "../../vendor/sulu/sulu/src/Sulu/Bundle/AdminBundle/Resources/js",
+    ],
    "dependencies": {
-        "sulu-admin-bundle": "file:../../vendor/sulu/sulu/src/Sulu/Bundle/AdminBundle/Resources/js"
+        "sulu-admin-bundle": "*",

This did end into the same problem, it did create the symlink in node_modules/sulu-admin-bundle to the given target directory but did not install its dependencies.

alexander-schranz avatar Jun 16 '21 21:06 alexander-schranz

I have 2 different workspaces for my library and app and I cannot use my local library inside my app as there is no node_modules in the dist folder of the library. The only workaround is to run npm install explicitly inside dist > project_folder and then fire npm link. But thats too hectic, I prefer going back to npm6

This should definitely get fixed or an option should be provided the least.

ui-daddy avatar Jun 20 '21 12:06 ui-daddy

Not sure how this is marked as an enhancement when this is breaking to projects that relied on the functionality of npm 6...

git-things avatar Jul 06 '21 19:07 git-things

@lang-eric because the whole point of a semver major bump is breaking changes, which means that re-adding it to v7 is minor or patch, since v7 never had it.

ljharb avatar Jul 06 '21 23:07 ljharb