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

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

当前回答

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

这很有魅力!

其他回答

这对我来说很有效:首先,确保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" : "*"
}

现在可以在包中指定本地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.

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

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

这很有魅力!

完整的本地开发有两个步骤:

提供包含包的本地目录的路径。

{ “名称”:“记者”, “依赖”:{ “酒吧”:“文件:. . / foo / bar” } }

符号链接包文件夹 CD ~/projects/node-redis #进入包目录 NPM link #创建全局链接 CD ~/projects/node-bloggy #进入其他包目录。 NPM link redis # link-install包