Martin
Martin

Reputation: 906

Run "npm install" as if package was not in workspace

I am working on an NPM workspace node project. To deploy one of the workspace's packages, I would like to run npm install and obtain a node_modules directory as a subdirectory of that package such that the package becomes self-contained.

Consider the directory structure below:

node_modules
packages
  ├ cloud-app
  │  ├ src
  │  └ package.json
  ├ helpers
  │  ├ src
  │  └ package.json
  ├ business-logic
  │  ├ src
  └  └ package.json
package.json

Just one deduplicated node_modules is excellent for development in a monorepo. But to deploy the cloud-app package, I need the structure to look like this:

packages
  ├ cloud-app
  │  ├ node_modules
  │  ├ src
  │  └ package.json
  ├ helpers
  │  ├ src
  │  └ package.json
  ├ business-logic
  │  ├ src
  └  └ package.json
package.json

Then, I could upload the cloud-app directory as usual without exposing my NPM workspace to the vendor's (incompatible) CD pipeline.

Is this possible at all? What would be the correct command or procedure here?

Upvotes: 8

Views: 4667

Answers (2)

MichaelG
MichaelG

Reputation: 737

This seems to work. Try the workspaces argument:

cd <my_project_root>
npm i --workspaces=false

https://docs.npmjs.com/cli/v9/commands/npm-install#workspaces

Upvotes: 9

Martin
Martin

Reputation: 906

While I did not find a standard way to achieve this, there is a slightly hacky way that worked for me:

Copying the node_modules directory allows the package to act as a stand-alone module. However, there is one caveat: The node_modules directory contains a symlink for each package in the workspace. Thus, a loop begins when it is copied into a package and when symlinks are followed. To prevent this, we first have to delete our package. Therefore, a deploy script could look something like this:

rm ./node_modules/cloud-app
cp -rL ./node_modules ./cloud-app/node_modules
# deploy cloud-app here

I thought of this while formulating the above question but would still be delighted to know whether there is any canonical, supported way to do this.

Upvotes: 0

Related Questions