node.js package.json 中的本地依赖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14381898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Local dependency in package.json
提问by user1680104
I want to do something like this, so npm installalso installs the package.jsonof ../somelocallibor more importantly its dependencies.
我想要做这样的事情,所以npm install也安装package.json的../somelocallib或更重要的是它的依赖。
"dependencies": {
"express": "*",
"../somelocallib": "*"
}
回答by danilopopeye
npm >= 2.0.0
npm >= 2.0.0
This feature was implementedin the version 2.0.0 of npm. Example:
这个特性是在 npm 2.0.0 版本中实现的。例子:
{
"name": "baz",
"dependencies": {
"bar": "file:../foo/bar"
}
}
Any of the following paths are also valid:
以下任何路径也有效:
../foo/bar
~/foo/bar
./foo/bar
/foo/bar
The local package will be copiedto the prefix(./node-modules).
npm < 2.0.0
npm < 2.0.0
Put somelocallibas dependency in your package.jsonas normal:
像往常somelocallib一样在您的依赖项中放置package.json:
"dependencies": {
"somelocallib": "0.0.x"
}
Then run npm link ../somelocalliband npm will install the version you're working on as a symlink.
然后运行npm link ../somelocallib,npm 将安装您正在使用的版本作为 symlink。
[email protected] /private/tmp/app
└── [email protected] -> /private/tmp/somelocallib
Reference: link(1)
参考:链接(1)
回答by Michael Trouw
It is now possible to specify local Node module installation paths in your package.jsondirectly. From the docs:
现在可以package.json直接在您的文件中指定本地 Node 模块安装路径。从文档:
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 -Sornpm install --save, using any of these forms:../foo/bar ~/foo/bar ./foo/bar /foo/barin 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.
本地路径
从版本 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 的测试,但不应在将包发布到公共注册表时使用。
回答by Brian McAuliffe
This works for me.
这对我有用。
Place the following in your package.json file
将以下内容放入您的 package.json 文件中
"scripts": {
"preinstall": "npm install ../my-own-module/"
}
回答by Taytay
If you want to further automate this, because you are checking your module into version control, and don't want to rely upon devs remembering to npm link, you can add this to your package.json "scripts" section:
如果您想进一步自动化此操作,因为您正在将模块检查到版本控制中,并且不想依赖于记住 npm 链接的开发人员,您可以将其添加到您的 package.json “脚本”部分:
"scripts": {
"postinstall": "npm link ../somelocallib",
"postupdate": "npm link ../somelocallib"
}
This feels beyond hacky, but it seems to "work". Got the tip from this npm issue: https://github.com/npm/npm/issues/1558#issuecomment-12444454
这感觉超出了hacky,但它似乎“有效”。从这个 npm 问题得到提示:https: //github.com/npm/npm/issues/1558#issuecomment-12444454
回答by sreekanth
This is how you will add local dependencies:
这是添加本地依赖项的方式:
npm install file:src/assets/js/FILE_NAME
npm install file:src/assets/js/FILE_NAME
Add it to package.json from NPM:
从 NPM 将其添加到 package.json 中:
npm install --save file:src/assets/js/FILE_NAME
npm install --save file:src/assets/js/FILE_NAME
Directly add to package.json like this:
像这样直接添加到 package.json 中:
....
"angular2-autosize": "1.0.1",
"angular2-text-mask": "8.0.2",
"animate.css": "3.5.2",
"LIBRARY_NAME": "file:src/assets/js/FILE_NAME"
....
回答by William Entriken
Master project
主项目
Here is the package.json you will use for the master project:
这是您将用于主项目的 package.json:
"dependencies": {
"express": "*",
"somelocallib": "file:./somelocallib"
}
There, ./somelocallibis the reference to the library folder as relative to the master project package.json.
在那里,./somelocallib是相对于主项目 package.json对库文件夹的引用。
Reference: https://docs.npmjs.com/files/package.json#local-paths
参考:https: //docs.npmjs.com/files/package.json#local-paths
Sub project
子项目
Handle your library dependencies.
处理您的库依赖项。
In addition to running npm install, you will need to run (cd node_modules/somelocallib && npm install).
除了跑步npm install,你还需要跑步(cd node_modules/somelocallib && npm install)。
This is a known bug with NPM.
这是 NPM 的一个已知错误。
Reference: https://github.com/npm/npm/issues/1341(seeking a more up-to-date reference)
参考:https: //github.com/npm/npm/issues/1341(寻求更新的参考)
Notes for Docker
Docker 的注意事项
Check in your master package.lockand your somelocallib/package.lockinto your source code manager.
签入您的主人package.lock和您somelocallib/package.lock的源代码管理器。
Then in your Dockerfile use:
然后在您的 Dockerfile 中使用:
FROM node:10
WORKDIR /app
# ...
COPY ./package.json ./package-lock.json ./
COPY somelocallib somelocallib
RUN npm ci
RUN (cd node_modules/zkp-utils/ && npm ci)
# ...
I use parenthesis in my (cd A && B)constructs to make the operation idempotent.
我在(cd A && B)构造中使用括号来使操作具有幂等性。
回答by itsazzad
Two steps for a complete local development:
完整本地开发的两个步骤:
回答by Sofia
This worked for me: first, make sure the npm directories have the right user
这对我有用:首先,确保 npm 目录具有正确的用户
sudo chown -R myuser ~/.npm
sudo chown -R myuser /usr/local/lib/node_modules
Then your in your package.json link the directory
然后你在你的 package.json 链接目录
"scripts": {
"preinstall": "npm ln mylib ../../path/to/mylib"
},
"dependencies": {
"mylib" : "*"
}
回答by H_I
I know that npm install ../somelocallibworks.
我知道那npm install ../somelocallib行得通。
However, I don't know whether or not the syntax you show in the question will work from package.json...
但是,我不知道您在问题中显示的语法是否适用于package.json...
Unfortunately, docseems to only mention URL as a dependency.
不幸的是,doc似乎只提到 URL 作为依赖项。
Try file:///.../...tar.gz, pointing to a zipped local lib... and tell us if it works.
尝试file:///.../...tar.gz,指向一个压缩的本地库...并告诉我们它是否有效。

