Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to specify local modules as npm package dependencies

Tags:

node.js

npm

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.

like image 362
Sam Adams Avatar asked Apr 04 '13 08:04

Sam Adams


People also ask

What is local installation of dependencies in npm?

npm install supports local directories and packagesjson 's dependencies. The local package definition will then include the file: prefix. Additionally, it'll create a symlink in your node_modules directory pointing to the local package. That saves a lot of work and is quickly done!


2 Answers

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.

like image 174
Randy the Dev Avatar answered Oct 25 '22 04:10

Randy the Dev


See: Local dependency in package.json

It looks like the answer is npm link: https://docs.npmjs.com/cli/link

like image 37
jasoncrawford Avatar answered Oct 25 '22 03:10

jasoncrawford