简体   繁体   English

运行“npm install”,就好像包不在工作区中一样

[英]Run "npm install" as if package was not in workspace

I am working on an NPM workspace node project.我正在开发一个NPM 工作区节点项目。 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.要部署工作区的包之一,我想运行npm install并获得一个node_modules目录作为该包的子目录,以便包变得独立。

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.只有一个去重的node_modules非常适合在 monorepo 中进行开发。 But to deploy the cloud-app package, I need the structure to look like this:但要部署cloud-app包,我需要如下结构:

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.然后,我可以像往常一样上传cloud-app目录,而不会将我的 NPM 工作区暴露给供应商的(不兼容的)CD 管道。

Is this possible at all?这可能吗? What would be the correct command or procedure here?这里正确的命令或程序是什么?

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.复制node_modules目录允许包充当独立模块。 However, there is one caveat: The node_modules directory contains a symlink for each package in the workspace.但是,有一个警告: node_modules目录包含工作区中每个包的符号链接。 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.我在提出上述问题时想到了这一点,但仍然很高兴知道是否有任何规范的、受支持的方法来做到这一点。

Something you can try is what @youzen brought on this post .你可以尝试的是@youzen 在这篇文章中带来的东西。

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.因此,node_modules 文件夹将在指定文件夹中创建。

You could access the link mentioned above to get others solutions, hope this helps!您可以访问上面提到的链接以获取其他解决方案,希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM