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

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

当前回答

实际上,从npm 2.0开始,现在已经支持本地路径了。

其他回答

在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')

这很有魅力!

这对我很有用。

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

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

NPM >= 2.0.0

该特性在npm 2.0.0版本中实现。本地路径可以使用npm install -S或npm install——save保存,使用以下任何一种形式:

../foo/bar
~/foo/bar
./foo/bar
/foo/bar

示例package.json:

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

npm ls:

app@0.0.1 /private/tmp/app
└── somelocallib@0.0.1 -> /private/tmp/somelocallib

NPM < 2.0.0

在包中放入somelocallib作为依赖项。正常的Json:

"dependencies": {
  "somelocallib": "0.0.x"
}

然后运行npm link ../somelocallib和NPM会将你正在使用的版本作为符号链接安装。

参考:链接(1)

我知道npm install ../ somelocallib作品。

然而,我不知道你在问题中显示的语法是否可以从package.json中工作…

不幸的是,doc似乎只提到URL作为依赖项。

尝试file:///…/…tar.gz,指向压缩的本地库…告诉我们它是否有效。

好奇……至少在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)都位于同一个级别.......