Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 621
Thomas Avatar asked Jul 04 '13 19:07

Thomas


2 Answers

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 ;)

like image 142
gustavohenke Avatar answered Oct 02 '22 14:10

gustavohenke


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.

like image 40
balupton Avatar answered Oct 02 '22 14:10

balupton