Tip: npm-installing directories

[2018-04-24] dev, javascript, nodejs, npm
(Ad, please don’t block)

Quick tip: npm lets you install directories, which is occasionally useful for organizing projects. For example, as a temporary step before uploading packages to npm (think lightweight npm link).

An example project  

As an example, let’s assume, we are writing a suite of Node.js-based CLI tools:

suite_of_tools/
  tool1/
  tool2/
  helpers/

Each of tool1, tool2 and helpers is an npm package with its own package.json and node_modules/.

npm-installing the directory helpers/  

npm lets you do:

$ cd tool1/
$ npm install ../helpers

Then npm does the following:

  • It installs the dependencies of helpers.
  • It adds a symlink to ../../helpers/ in tool1/node_modules.
  • It adds an entry to the dependencies:
    "dependencies": {
      "helpers": "file:../helpers",
      ···
    }
    

Et voilà! Now tool1’s “sibling dependency” on helpers will be (re)created whenever you do an npm install for tool1.