how to specify local modules as npm package dependencies

node.jsNpm

node.js Problem Overview


I have an application which has the usual set of dependencies on third party modules (e.g. 'express') specified in the package.json file under dependencies. E.g.

"express"     : "3.1.1"

I would like to structure my own code modularly and have a set of local (meaning on the file system i am currently in) modules be installed by the package.json. I know that i can install a local module by running:

npm install path/to/mymodule

However, I don't know how to make this happen via the package.json dependencies structure. Using the --save option in this command is simply putting "mymodule": "0.0.0" into my package.json (doesn't reference the filepath location). If i then remove the installed version from node_modules, and try to re-install from the package.json, it fails (because it looks for "mymodule" in the central registry, and doesn't look locally).

I'm sure the is a way of telling the "dependencies": {} structure that I want it to be installed from a file system path, but don't know how.

Anyone else had this problem? Thanks.

node.js Solutions


Solution 1 - node.js

npm install now supports this
npm install --save ../path/to/mymodule

For this to work mymodule must be configured as a module with its own package.json. See Creating NodeJS modules.

As of npm 2.0, local dependencies are supported natively. See danilopopeye's answer to a similar question. I've copied his response here as this question ranks very high in web search results.

>This feature was implemented in the version 2.0.0 of npm. For example:

{
  "name": "baz",
  "dependencies": {
    "bar": "file:../foo/bar"
  }
}

>Any of the following paths are also valid:

../foo/bar
~/foo/bar
./foo/bar
/foo/bar
syncing updates

Since npm install <folder> adds the package in the directory as a symlink in the current project any changes to the local package are automatically synced.

Solution 2 - node.js

Solution 3 - node.js

I couldn't find a neat way in the end so I went for create a directory called local_modules and then added this bashscript to the package.json in scripts->preinstall

#!/bin/sh
for i in $(find ./local_modules -type d -maxdepth 1) ; do
    packageJson="${i}/package.json"
    if [ -f "${packageJson}" ]; then
        echo "installing ${i}..."
        npm install "${i}"
    fi
done

Solution 4 - node.js

After struggling much with the npm link command (suggested solution for developing local modules without publishing them to a registry or maintaining a separate copy in the node_modules folder), I built a small npm module to help with this issue.

The fix requires two easy steps.

First:

npm install lib-manager --save-dev

Second, add this to your package.json:

{  
  "name": "yourModuleName",  
  // ...
  "scripts": {
    "postinstall": "./node_modules/.bin/local-link"
  }
}

More details at https://www.npmjs.com/package/lib-manager. Hope it helps someone.

Solution 5 - node.js

You can just add to your package.json file in your project

"package-name" : "path/to/package"

and then run npm i in your project

Solution 6 - node.js

If it's acceptible to simply publish your modules preinstalled in node_modules alongside your other files, you can do it like this:

// ./node_modules/foo/package.json
{ 
  "name":"foo",
  "version":"0.0.1",
  "main":"index.js"
}

// ./package.json
...
"dependencies": {
  "foo":"0.0.1",
  "bar":"*"
}

// ./app.js
var foo = require('foo');

You may also want to store your module on git and tell your parent package.json to install the dependency from git: https://npmjs.org/doc/json.html#Git-URLs-as-Dependencies

Solution 7 - node.js

At work we have a common library that is used by a few different projects all in a single repository. Originally we used the published (private) version (npm install --save rp-utils) but that lead to a lot of needless version updates as we developed. The library lives in a sister directory to the applications and we are able to use a relative path instead of a version. Instead of "rp-utils": "^1.3.34" in package.json it now is:

{ 
  "dependencies": { ...
    "rp-utils": "../rp-utils",
   ...

the rp-utils directory contains a publishable npm package

Solution 8 - node.js

use local-install

I had issues with conflicting react installations from the local dependency. I solved the error by using local-install npm package. This package does not create symlinks, which solved my issue.

Steps:

  1. run npm i -g local-install
  2. run npx install-local --save <local-path> inside the target repository to install the local dependency

Further reading: https://www.npmjs.com/package/install-local

The error I received, when trying to install the local package with npm install --save <local-directory>:

> Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: > 1. You might have mismatching versions of React and the renderer (such as React DOM) > 2. You might be breaking the Rules of Hooks > 3. You might have more than one copy of React in the same app

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSam AdamsView Question on Stackoverflow
Solution 1 - node.jsRandy the DevView Answer on Stackoverflow
Solution 2 - node.jsjasoncrawfordView Answer on Stackoverflow
Solution 3 - node.jsSam AdamsView Answer on Stackoverflow
Solution 4 - node.jsAnurag DuttaView Answer on Stackoverflow
Solution 5 - node.jsjoe abou nakkoulView Answer on Stackoverflow
Solution 6 - node.jsPlatoView Answer on Stackoverflow
Solution 7 - node.jsJoshua GoldsteinView Answer on Stackoverflow
Solution 8 - node.jswasserholzView Answer on Stackoverflow