简体   繁体   中英

NPM package as nested dependency of self

I have an NPM package (package A) that compiles itself with the last stable version of itself. It does this through an intermediary Grunt task (package B) that itself depends on package A. Thus, the dependency chain is:

Package A -> Package B (as devDependency ) -> Package A (as 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.

What's the least-hacky/recommended way of installing the child Package A? 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.

What about running making an js file for such a task?

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
  2. Add the following to your package.json files:

     "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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