4

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?

Martin
  • 381
  • 4
  • 17

3 Answers3

5

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

MichaelG
  • 652
  • 1
  • 10
  • 17
  • 2
    This did exactly what I needed. The `node_modules` folder of the package was populated with all of the package's direct dependencies and a `package-lock.json` file was generated in the package folder. – Mark Whitfeld Jan 19 '23 at 20:04
0

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.

Martin
  • 381
  • 4
  • 17
0

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!

  • This, unfortunately, does not work in a workspace. (at least when I tried it) This might have to do with NPM's altered dependency resolution process for workspaces. – Martin Jul 06 '22 at 09:37
  • @Martin Try this one, if works I'll update my answer. On the root folder of your project run this command:```npm --prefix ./cloud-app install .``` , knowing that the prefix path will be de destiny of the node_modules folder. – Bernardo Moraes Jul 06 '22 at 23:18