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

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

当前回答

用纱线可以这样做

纱线添加文件:../somelocallib

其他回答

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

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

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

这对我来说很有效:首先,确保npm目录有正确的用户

sudo chown -R myuser ~/.npm
sudo chown -R myuser /usr/local/lib/node_modules

那你就在你的包裹里。Json链接目录

"scripts": {
 "preinstall": "npm ln mylib ../../path/to/mylib"
}, 
"dependencies": {
  "mylib" : "*"
}

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