简体   繁体   English

如何找到由NPM软件包的某个依赖项安装的二进制文件,以便可以执行它?

[英]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.

相关问题 如何将我通过npm安装的所有依赖项保存到我的package.json文件中? - How do I save all the dependencies I install through npm into my package.json file? 预先构建的NPM软件包:如何使用户摆脱依赖关系? - Pre-built NPM package: How can I spare users my dependencies? 如何卸载使用 npm 链接安装的 package? - How do I uninstall a package installed using npm link? 如何创建包含软件包及其依赖项的NPM存储库 - How can I create an NPM repository containing a package and its dependencies 如何从已经安装的NPM软件包中运行脚本? - How can I run a script from an already installed NPM package? 如何打包Node Web应用程序以便可以将其安装为本地应用程序? - How do I package up a Node web app so it can be installed as a local app? 如何与我的应用一起开发已安装npm的软件包? - How do I develop npm installed packages along with my app? 如何在Meteor 1.3中安装带有npm的软件包,以便其他npm库可以要求它 - Where do I install a package with npm in Meteor 1.3 so that other npm libraries can require it 使用nodejs / npm的请求包进行发布时,如何发布普通文件缓冲区而不是二进制编码文件? - How do I post a plain file buffer instead of binary-encoded-file when posting with nodejs/npm's request package? 使用 npm 如何将包下载为 zip 包,其中包含包中包含的所有依赖项 - Using npm how can I download a package as a zip with all of its dependencies included in the package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM