bash npm 包中的二进制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24082810/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:37:58  来源:igfitidea点击:

Binary file in npm package

javascriptnode.jsbashbinarynpm

提问by ciembor

I try to create an npm package, which can be started as a command from shell. I have package.json

我尝试创建一个 npm 包,它可以从 shell 中作为命令启动。我有package.json

{
  "name": "myapp",
  "version": "0.0.6",
  "dependencies": {
    "async": "",
    "watch": "",
    "node-promise": "",
    "rmdir": "",
    "should": "",
    "websocket": ""
  },
  "bin": "myapp"
}

and myapp

myapp

#!/bin/bash

path=`dirname "
module.js:340
    throw err;
          ^
Error: Cannot find module '/usr/local/bin/myapp.js'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
"` file="/myapp.js" node $path$file &

But I get an error:

但我收到一个错误:

"bin": { "myapp" : "<relative_path_to_myapp.js>/lib/myapp.js" }

The problem is that myapp.js is in another directory. How can I get this directory name from my script? Or maybe there is better way to do this?

问题是 myapp.js 在另一个目录中。如何从我的脚本中获取此目录名称?或者也许有更好的方法来做到这一点?

回答by 3329

Actually, you can put your myapp.jsfile into bin.
So, the bin key in package.json file should be like this :

实际上,您可以将myapp.js文件放入bin.
所以,package.json 文件中的 bin 键应该是这样的:

#!/usr/bin/env node

At the first line in myapp.js, you must add this shebang line :

在 中的第一行myapp.js,您必须添加此shebang 行:

#!/usr/bin/env node

var myapp = require('<relative_path_to_myapp.js>/myapp.js');
myapp.doSth();

It tells the system to use nodeto run myapp.js.

它告诉系统使用node运行myapp.js.



... Or if you don't want to call myapp.jsdirectly, you can create a script like this to be your executable file :

...或者如果你不想myapp.js直接调用,你可以创建一个这样的脚本作为你的可执行文件:

"bin" : { "myapp" : "<relative_path_to_the_script>/script.js" }

and in package.json :

在 package.json 中:

myapp_path=$( npm explore -g myapp -- "pwd" )

By doing this either way, you can avoid finding the path to your nodemodule.

通过以任何一种方式执行此操作,您都可以避免找到节点模块的路径。



But... if you insist to use your old myappbash script, then you can find the path to the module with this :

但是...如果您坚持使用旧的myappbash 脚本,那么您可以使用以下命令找到模块的路径:

{ "bin" : { "myapp" : "./cli.js" } }

Hope these help :D

希望这些帮助:D

回答by Mir-Ismaili

https://docs.npmjs.com/files/package.json#bin

https://docs.npmjs.com/files/package.json#bin

From the above link:

从上面的链接:

...

To use this, supply a binfield in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/binfor global installs, or ./node_modules/.bin/for local installs.

For example, myapp could have this:

{ "bin" : { "myapp" : "./cli.js" } }

So, when you install myapp, it'll create a symlink from the cli.jsscript to /usr/local/bin/myapp.

...

...

要使用它,请在 package.json 中提供一个bin字段,它是命令名到本地文件名的映射。在安装时,npm 会将该文件符号链接到prefix/bin以进行全局安装,或者./node_modules/.bin/进行本地安装。

例如,myapp 可能有这个:

##代码##

因此,当您安装 myapp 时,它会创建一个从cli.js脚本到/usr/local/bin/myapp的符号链接。

...