npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.46k stars 3.15k forks source link

[BUG] `npm i` creates wrong symlinks inside workspaces #4921

Open iyegoroff opened 2 years ago

iyegoroff commented 2 years ago

Is there an existing issue for this?

This issue exists in the latest npm version

Current Behavior

When workspace packages/bar has workspace packages/foo as a dependency npm i creates invalid foo symlink inside packages/bar/node_modules folder.

Folder structure and packages/bar/package.json file:

screenshot

readlink packages/bar/node_modules/foo prints ../packages/foo.

Expected Behavior

npm i creates valid symlinks inside workspace node_modules folder.

readlink packages/bar/node_modules/foo prints ../../../packages/foo.

Steps To Reproduce

Repro is here https://github.com/iyegoroff/npm-workspaces-symlink-bug

npm i
npm start -w packages/bar

The output will be an error: sh: ./node_modules/foo/index.js: No such file or directory. Correct output should be foo works!.

This also reproduces when running Github Actions.

Environment

issuefiler commented 2 years ago

Unlike my case, https://github.com/npm/cli/issues/4994, your case is not a bug.

Your package.json is in packages/bar/ and referring to packages/foo/. Since it’s a relative path, your package.json is clearly referring to packages/bar/packages/foo/. For your installed foo, packages/bar/node_modules/foo, this is ../packages/foo/.

issuefiler commented 2 years ago

If you want to refer to package/foo/ (an absolute path) from package/bar/ (an absolute path), use the relative path file:../foo/.

You’ll face the real bug #4994, though.