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/"
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值