Home > OS >  Run "npm install" as if package was not in workspace
Run "npm install" as if package was not in workspace

Time:07-06

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?

CodePudding user response:

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.

CodePudding user response:

Something you can try is what @youzen brought on this post.

Basically, in the root folder of your repository, you can run:

npm install --prefix <path/to/prefix_folder> -g

And so, the node_modules folder will be created in the specified folder.

You could access the link mentioned above to get others solutions, hope this helps!

  • Related