我想这样做,所以npm install也会安装这个包。Json的../somelocallib或者更重要的是它的依赖项。

"dependencies": {
    "express": "*",
    "../somelocallib": "*"
}

当前回答

现在可以在包中指定本地Node模块安装路径。直接json。从文档中可以看出:

Local Paths As of version 2.0.0 you can provide a path to a local directory that contains a package. Local paths can be saved using npm install -S or npm install --save, using any of these forms: ../foo/bar ~/foo/bar ./foo/bar /foo/bar in which case they will be normalized to a relative path and added to your package.json. For example: { "name": "baz", "dependencies": { "bar": "file:../foo/bar" } } This feature is helpful for local offline development and creating tests that require npm installing where you don't want to hit an external server, but should not be used when publishing packages to the public registry.

其他回答

在2021年,你需要这样使用它:

npm i my-pkg@file:./path-to-my-pkg.js

# To remove it later
npm un my-pkg

如果是完整的package.json,则在最后使用.js。

使用

const myPkg = require('my-pkg')

这很有魅力!

好奇……至少在Windows上(我的npm是3.something),我需要做:

"dependencies": {
 "body-parser": "^1.17.1",
 "module1": "../module1",
 "module2": "../module2",

当我做npm安装../module1——保存它会导致绝对路径,而不是文档中的相对路径。

我又瞎折腾了一下,然后决定…/xxx就足够了。

具体地说,我有本地节点模块检出说d:\build\module1, d:\build\module2和我的节点项目(应用程序)在d:\build\nodeApp。

为了“安装”,我:

d:\build\module1> rmdir "./node_modules" /q /s && npm install
d:\build\module2> rmdir "./node_modules" /q /s && npm install
d:\build\nodeApp> rmdir "./node_modules" /q /s && npm install

module1的包。Json有一个依赖项"module2": "../module2";Module2没有本地依赖;nodeApp有依赖"module1": ".."../ module1"和"module2": "../module2"。

不确定这是否只适用于我,因为所有3个文件夹(module1, module2和nodeApp)都位于同一个级别.......

在2020年的这里,我正在用Windows 10操作系统工作

"dependencies": {
    "some-local-lib": "file:../../folderY/some-local-lib" 
    ...
}

然后进行npm安装。结果是在节点模块中创建文件夹的快捷方式。 这行不通。你需要一个硬链接-哪个窗口支持,但是 你必须在Windows中做一些额外的事情来创建一个硬符号链接。

因为我不是真的想要一个硬链接,我尝试使用一个url代替:

"dependencies": {
    "some-local-lib": "file:///D:\\folderX\\folderY\\some-local-lib.tar" 
     ....
}

这个很好用。 tar(你必须tar库的build / dist文件夹中的东西)被提取到node-modules中的一个真正的文件夹中,你可以像其他东西一样导入。 显然tar部分有点烦人,但由于'some-local-lib'是一个库(无论如何都要构建),我更喜欢这个解决方案,而不是创建一个硬链接或安装一个本地npm。

这对我很有用。

将以下内容放入包中。json文件

"scripts": {
    "preinstall": "npm install ../my-own-module/"
}

如果你想进一步自动化这个,因为你正在将你的模块检入版本控制,并且不想依赖于开发人员记住npm link,你可以将这个添加到你的包中。Json“脚本”部分:

"scripts": {
    "postinstall": "npm link ../somelocallib",
    "postupdate": "npm link ../somelocallib"
  }

这感觉很俗气,但似乎很“管用”。从这个npm问题中得到了提示: https://github.com/npm/npm/issues/1558#issuecomment-12444454