[英]How do I locate a binary installed by one of my NPM package's dependencies so I can execute it?
I have an NPM package which depends on node-pg-migrate
. 我有一个依赖于
node-pg-migrate
的NPM软件包。 From inside my package, I need to execute node-pg-migrate
's binary pg-migrate
. 从我的包内部,我需要执行
node-pg-migrate
的二进制pg-migrate
。 I'm running node 0.12.13. 我正在运行节点0.12.13。
If the app I've installed my package in doesn't also depend on node-pg-migrate
, this is trivial. 如果我安装了我的软件包的应用也不依赖于
node-pg-migrate
,那就太简单了。 The dependency is installed in a node_modules
directory inside my package. 依赖关系安装在我的包内的
node_modules
目录中。
- node_modules
| - my-package
| - node_modules
| - node-pg-migrate
Here's what I would do in that case 在这种情况下,我会这样做
exec('./node_modules/node-pg-migrate/bin/pg-migrate up',
function(error, stdout, stderr) {
// do something
}
);
However, if the app I'm installing my package to also depends on node-pg-migrate
, it will instead be installed in the app's node_modules
directory alongside my package. 但是,如果我要安装我的软件包的应用程序也依赖于
node-pg-migrate
,它将安装在我的软件包旁边的应用程序的node_modules
目录中。
- node_modules
| - my-package
| - node-pg-migrate
I've thought about first checking my package's node_modules
for pg-migrate
and backing out one level if it isn't there, but that breaks down if my package is an inner dependency. 我考虑过先检查程序包的
node_modules
是否存在pg-migrate
,然后在不存在的情况node_modules
一个级别,但是如果我的程序包是内部依赖项,那将导致崩溃。 Then, I might have to try going out one more level. 然后,我可能不得不再尝试一个级别。
- node_modules
| - node-pg-migrate?
| - some-package
| - node_modules
| - node-pg-migrate?
| - my-package
| - node_modules
| - node-pg-migrate?
How can I find the location of the pg-migrate
binary and run it no matter where it falls in the dependency tree? 我如何找到
pg-migrate
二进制文件的位置并运行它,无论它位于依赖树的何处?
Since you're executing the pg-migrate
binary from the command line, it's recommended that you install it globally 由于您是从命令行执行
pg-migrate
二进制文件,因此建议您全局安装
From the npm-folders documentation : 从npm-folders文档中 :
- Local install (default): puts stuff in ./node_modules of the current package root.
本地安装(默认):将内容放入当前软件包根目录的./node_modules中。
- Global install (with -g): puts stuff in /usr/local or wherever node is installed.
全局安装(带-g):将内容放入/ usr / local或安装节点的任何位置。
- Install it locally if you're going to require() it.
如果您需要require(),请在本地安装。
- Install it globally if you're going to run it on the command line.
如果要在命令行上运行它,请全局安装它。
- If you need both, then install it in both places, or use npm link.
如果两者都需要,则将其安装在两个地方,或使用npm链接。
With a global installation you would not need to bother where the package was installed and would be able to execute the migrate command in this manner: 使用全局安装,您无需打扰软件包的安装位置,并且能够以这种方式执行migration命令:
exec('pg-migrate up',
...
);
npm installs links to the various executables within node_modules in the .bin
directory, so you don't need to go look for them in specific sub-directory. npm在
.bin
目录中的node_modules中安装指向各种可执行文件的链接,因此您无需在特定的子目录中查找它们。
So to get to the path you need, you can simply do this: 因此,要找到所需的路径,只需执行以下操作:
const myExePath = __dirname + '/node_modules/.bin/my-exe';
const binDir = execFileSync('npm', ['bin']).toString().trim();
exec(binDir + ' pg-migrate up',
function(error, stdout, stderr) {
// do something
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.