Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM 7 workspaces - how to install new package in workspace?

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.

like image 854
Dave Welling Avatar asked Dec 10 '20 15:12

Dave Welling


People also ask

How do I add packages to devDependencies?

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.

How do npm workspaces work?

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.

Do I need to install npm packages for every project?

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.


1 Answers

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.

like image 54
mattwad Avatar answered Sep 23 '22 01:09

mattwad