If I have a NPM 7 workspace like this:
root - submodule0 - submodule1 - submodule2
and I navigate to the submodule0 directory and run npm i somepackage
it seems to "break" the workspace by creating a new package-lock.json in the submodule0 directory and installing all the dependencies there. In other words, it just does the old behavior that existed before I created the workspace. I was hoping for a command similar to lerna where I can install a new package in submodule0 from the root. Something like:
npm i somepackage --scope submodule0
So far, the only workaround I can find is to edit the submodule0 package.json and add the somepackage
manually. Then run npm i
from the root. Obviously this is not ideal because I need to look up the @latest version, navigate to the subdirectory, open the package.json, etc. etc. as opposed to just typing one line in the root.
To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install ) or the --save-dev flag for devDependencies.
Workspaces is a generic term that refers to the set of features in the npm cli that provides support to managing multiple packages from your local file system from within a singular top-level, root package.
In general, all packages should be installed locally. This makes sure you can have dozens of applications in your computer, all running a different version of each package if needed.
Workspace support for npm install
and npm uninstall
was added in npm v7.14.0. You can now just do:
npm i somepackage --workspace=submodule0
Uninstalling modules has been the biggest pain, so this is really exciting. The npm team seems to be slowly adding support to commands one by one. Follow updates here: https://github.com/npm/cli/blob/latest/CHANGELOG.md.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With