ChatGPT解决这个技术问题 Extra ChatGPT

How to specify local modules as npm package dependencies

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.

A really good question. Sad to realise that there is no feature equivalent for package.json to what we have in Gemfiles.
possible duplicate of Local dependency in package.json

A
Anjum....

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.


This worked for me. (I just did a local relative path like "mymodule":"file:mymoduledir"
npm install --save ../my-local-repo
And how to use it in the project? I'm trying to call it like import { HelloWorld } from "my-test-lib";, but i receive "Cant find module" error. Please, take a look at stackoverflow.com/questions/46818083/…
@LucioMollinedo can you share the syntax of how you imported the local module? As with Vitallii, I'm getting "Can't fine module" error with import { HelloWorld } from "my-test-lib";
This doesn't work the same as referencing a package as dependencies won't be installed to the project
g
gman

See: Local dependency in package.json

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


actually npm link will only create the symlinks, and will not modify the package.json to add the local dependency
But if its not a symlink how will the parent project know to rebuild once the dependency has finished building?
S
Sam Adams

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

C
Clay Bridges

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.


j
joe abou nakkoul

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


P
Plato

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


Unfortunately that would involve node_modules having my local modules and third party/contributed modules installed from the registry (e.g. connect) in the same directory. Besides that being confusing from a Git/VCS perspective (i.e. would have to ignore all in node_modules except those i created), it's also bad practice (those I have written and aren't published should be kept separate from those others have written and published).
When I add a local module then make changes these are not seen by my main app. Why is this the case?
J
Joshua Goldstein

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


do you need to perform npm install every time you make changes to rp-utils?
Any time you change the imported package you will need to rebuild it and then in the application that uses it npm update rp-utils update that dependency.
w
wasserholz

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:

run npm i -g local-install run npx install-local --save 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: You might have mismatching versions of React and the renderer (such as React DOM) You might be breaking the Rules of Hooks You might have more than one copy of React in the same app


It's npm i install-local not local-install
npx install-local is the correct command. I got the words flipped.