0

Assume that the package foo depends on the package bar, both packages are in the same monorepo and being developed in parallel. Since npm allows installing the local dependencies by relative paths, the bar in the package.json of foo can be specified as:

{
  "name": "foo",
  "version": "0.0.0"
  "devDependencies": {
    "bar": "../bar"
  }
}

Assume also that bar is the peer dependency of foo:

{
  "name": "foo",
  "version": "0.0.0"
  "peerDependencies": {
    "bar": "0.0.0"
  }
  "devDependencies": {
    "bar": "../bar"
  }
}

Now, suppose that the version 0.0.0 of foo package has been published. How to replace the "../bar" reference in package.json of foo with 0.0.0 and install the dependency from npm instead of symlink?

  • If just execute npm install [email protected] -E, the symlink will not be replaced with the distribution from npm.
  • If we execute npm uninstall bar && npm install bar -D -E, the bar will be deleted from the peerDependencies too, thus we need manually edit the package.json to restore it after installation. Not impossible to automate it, but not very simple.

Assume that only npm may be used, no third-party utils like Lerna, Yarn workspaces, etc.

3
  • remove bar from devDependencies, remove node_modules, reinstall? Commented Oct 15 at 11:09
  • @EstusFlask Thank you for the comment. As I have said in question field, "if we execute npm uninstall bar && npm install bar -D -E, the "bar" will be deleted from the peerDependencies too, thus we need manually edit the package.json to restore it after installation. Not impossible to automate it, but not very simple. Commented Oct 15 at 11:29
  • This isn't what I said. You need to remove it only from a section where it's not needed (devDependencies and not peerDependencies in your case) Commented Oct 15 at 11:30

1 Answer 1

1

Remove bar from devDependencies only if this is the intention. Then remove the entire node_modules and reinstall because partial module installation is always error-prone.

In case this needs to be automated, cross-platform command is:

npm pkg delete devDependencies.bar && npx -y rimraf node_modules && npm i
Sign up to request clarification or add additional context in comments.

2 Comments

Just tried your solution. Do you mean npm i bar -D -E instead of simply npm i? With your command, the bar will be completely uninstalled. Also, maybe there is no need to uninstall whole node_modules. I have executed npx -y rimraf node_modules/bar and it was enough (if I have not missed some side effects).
I meant exactly this command, did you have problems with it? npm pkg delete devDependencies.bar removes bar from devDependencies but not peerDependencies. Then you need to remove it physically from node modules. You could do npx -y rimraf node_modules/bar && npm i --no-save bar, but as I said, partial installation is error-prone. I had numerous bugs with npm that was caused by this. Doing this from scratch saves time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.