我是靠谱客的博主 优雅发带,最近开发中收集的这篇文章主要介绍package.json中的本地依赖项,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本文翻译自:Local dependency in package.json

I want to do something like this, so npm install also installs the package.json of ../somelocallib or more importantly its dependencies. 我想做这样的事情,所以npm install也会安装../somelocallibpackage.json或更重要的是它的依赖项。

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

#1楼

参考:https://stackoom.com/question/yLO6/package-json中的本地依赖项


#2楼

npm >= 2.0.0 npm> = 2.0.0

This feature was implemented in the version 2.0.0 of npm. 此功能已在npm的2.0.0版本中实现 。 Example: 例:

{
"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 copied to the prefix ( ./node-modules ). 本地包将被复制到前缀 ( ./node-modules )。

npm < 2.0.0 npm <2.0.0

Put somelocallib as dependency in your package.json as normal: 正常地将somelocallib作为依赖项放入package.json中:

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

Then run npm link ../somelocallib and npm will install the version you're working on as a symlink . 然后运行npm link ../somelocallib将以符号 npm link ../somelocallib安装您正​​在使用的版本。

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

Reference: link(1) 参考: link(1)


#3楼

I know that npm install ../somelocallib works. 我知道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, doc seems to only mention URL as a dependency. 不幸的是, 文档似乎只提到URL作为依赖项。

Try file:///.../...tar.gz , pointing to a zipped local lib... and tell us if it works. 尝试file:///.../...tar.gz ,指向一个压缩的本地库...,然后告诉我们它是否有效。


#4楼

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 从此npm问题获得了提示: https : //github.com/npm/npm/issues/1558#issuecomment-12444454


#5楼

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" : "*"
}

#6楼

This works for me. 这对我有用。

Place the following in your package.json file 将以下内容放入您的package.json文件中

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

最后

以上就是优雅发带为你收集整理的package.json中的本地依赖项的全部内容,希望文章能够帮你解决package.json中的本地依赖项所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部