简体   繁体   English

NPM包作为self的嵌套依赖

[英]NPM package as nested dependency of self

I have an NPM package (package A) that compiles itself with the last stable version of itself. 我有一个NPM包(包A),它自己编译自己的最后一个稳定版本。 It does this through an intermediary Grunt task (package B) that itself depends on package A. Thus, the dependency chain is: 它通过中间Grunt任务(包B)来实现,该任务本身依赖于包A.因此,依赖链是:

Package A -> Package B (as devDependency ) -> Package A (as dependency ) 包A - >包B(作为devDependency ) - >包A(作为dependency

However, when Package A is installed through npm install , NPM won't install Package A as a dependency of Package B, presumedly by design - I assume it's trying to prevent circular dependencies, even though because Package B is only a devDependency , it won't be installed on the child Package A anyways. 但是,当通过npm install安装Package A时,NPM不会将Package A安装为Package B的依赖项,假设是设计的 - 我假设它试图阻止循环依赖,即使因为Package B只是一个devDependency ,它赢了无论如何都要安装在儿童套餐A上。

What's the least-hacky/recommended way of installing the child Package A? 什么是安装儿童套餐A最少的hacky /推荐方式? My first solution was to just add an postinstall script that simply ran cd node_modules/package-B && npm install package-A , but this breaks because the CWD of postinstall isn't always the package's root directory. 我的第一个解决方案是添加一个简单地运行cd node_modules/package-B && npm install package-Apostinstall脚本,但这会破坏,因为postinstall的CWD并不总是包的根目录。

What about running making an js file for such a task? 那么为这样的任务运行制作一个js文件怎么样?

var spawn = require("child_process").spawn;
spawn("npm", [ "install", "package-A" ], {
  cwd: process.cwd() + "/node_modules/package-B/",
  env: process.env
});

I'm not sure whether this will work, but maybe it inspire you to do more things with it ;) 我不确定这是否有效,但也许它激励你用它做更多的事情;)

Figured out a nice automated way of doing this: 想出了一个很好的自动化方法:

  1. Add this file to your project: cyclic.js 将此文件添加到项目中: cyclic.js
  2. Add the following to your package.json files: 将以下内容添加到package.json文件中:

     "scripts": { "preinstall": "node ./cyclic.js" } 

With this solution, when you run npm install it will automatically force install the cyclic depencies for you without error. 使用此解决方案,当您运行npm install ,它将自动强制为您安装循环依赖项而不会出现错误。

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

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