准备一个独立的目录,以在使用 npm 工作区的 monorepo 中部署包。

huangapple go评论67阅读模式
英文:

Prepare standalone directory to deploy package in a monorepo using npm workspaces

问题

我有一个使用npm工作区的单体库。

root/
  app/
    package.json
  server/
    package.json
  store/
    package.json
  utils/
    package.json

app/package.json 文件引用了私有的未发布的依赖项,像这样:

{
  "dependencies": {
    "@my-scope/server": "file:../server",
    "@my-scope/store": "file:../store",
    "@my-scope/utils": "file:../utils"
  }
}

当在本地运行时,这是正常工作的;npm会将所有的 node_modules 依赖项提升到存储库的顶部,并在依赖包之间添加符号链接。但是,一旦我尝试生成一个用于部署到Azure的.zip文件归档,问题就出现了。当然,我不想部署整个单体库,只想部署我的应用程序包。然而,由于所有的node_modules依赖项都提升到了父目录,这将无法正常工作。

我尝试在应用程序目录中运行以下命令:

npm install --workspaces=false

它生成了接近我所需要的内容:

app/
  node_modules/
    direct-dependency-a/
    direct-dependency-b/
    @my-scope/
      server/ -> ../../../server
      store/ -> ../../../store
      utils/ -> ../../../utils

...但它不会安装@my-scope/*包的任何依赖项。

如何从npm工作区单体库中生成一个完全独立的部署准备好的目录?

英文:

I have a monorepo that uses npm workspaces.

root/
  app/
    package.json
  server/
    package.json
  store/
    package.json
  utils/
    package.json

The app/package.json file references private non-published dependencies in its package.json like this:

{
  "dependencies": {
    "@my-scope/server": "file:../server",
    "@my-scope/store": "file:../store",
    "@my-scope/utils": "file:../utils"
  }
}

This is working fine when running locally; npm hoists all the node_modules dependencies to the top of the repo and adds symlinks between our dependent packages. However, things break once I try to produce a .zip file archive for deploying to Azure. I of course don't want to deploy my entire monorepo, just my app package. However, with all the node_modules dependencies hoisted up to the parent directory, this won't work.

I've tried running this inside of the app directory:

npm install --workspaces=false

It produces this which is close to what I'm looking for:

app/
  node_modules/
    direct-dependency-a/
    direct-dependency-b/
    @my-scope/
      server/ -> ../../../server
      store/ -> ../../../store
      utils/ -> ../../../utils

...but it does not install any of the dependencies of the @my-scope/* packages.

How can I produce a completely standalone deployment-ready directory from a npm workspaces monorepo?

答案1

得分: 0

我找到了钥匙。除了使用 --workspaces=false 运行安装之外,将 --install-links 选项添加到 npm install 命令会使包正确安装。它会复制 monorepo 包中的文件而不是链接它们,并安装其依赖项。

注意:正如 @RafaelLeite 在评论中指出的那样,这不使用锁定文件,因为您的锁定文件位于monorepo的根目录,这可能会导致在安装时出现“无监督”的版本升级,因此这仍然不是一个完美的解决方案。

英文:

I found the key. In addition to running an install with --workspaces=false, adding the --install-links option to the npm install command causes the packages to be installed correctly. It copies the files from the monorepo package instead of linking, and it installs its dependencies as well.

Caveat: as @RafaelLeite points out in the comments, this doesn't use lockfiles since your lockfile is hosted at the root of the monorepo, and that can cause "unsupervised" version bumps when installing, so this is still not a perfect solution.

huangapple
  • 本文由 发表于 2023年6月26日 05:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定