[英]How to make a nodejs script be a CLI tool in linux?
I made a javascript file mytool.js
, it has some dependencies (in package.json). 我制作了一个javascript文件mytool.js
,它具有一些依赖性(在package.json中)。
Now I can execute typing in 现在我可以输入
~/mynodes/mytool $ node mytool
But if I change the working directory I can't use this command anymore because previously it was run locally. 但是,如果更改工作目录,则无法再使用此命令,因为以前它是在本地运行的。 What I want to achieve is to be able to just type : 我想要实现的是能够输入:
~$ mytool
(wherever I am in my system's filesystem and without typing node
before). (无论我在系统文件系统中的任何位置,并且之前都没有键入node
)。
Should I install it manually ? 我应该手动安装吗?
If yes, where is the common location to install a personal nodejs script in a unix-like system ? 如果是,在类似Unix的系统中安装个人nodejs脚本的通用位置在哪里?
Or is there a npm-like command to install a personal script system wide ? 还是有类似npm的命令来在整个系统范围内安装个人脚本?
When you add a "bin" key in your package.json: 在package.json中添加“ bin”键时:
"bin": {
"mytool": "mytool.js"
},
then you will be able to install your script with npm install -g
and it will be automatically added where it should be (to a place where other globally installed cli tools are installed, which should be in your PATH). 那么您将能够使用npm install -g
安装脚本,并且该脚本会自动添加到应该安装的位置(安装到其他全局安装的cli工具的安装位置,该位置应该在PATH中)。
You can see this simple project as an example: 您可以以这个简单的项目为例:
It was created as an example for this answer but it does what you need: 它是作为此答案的示例创建的,但它可以满足您的需求:
Note that you don't need to publish your script to npm to be able to install it - though you can do it, or you can also install projects directly from GitHub (including private repos) - but you can also install a module that you have in your local directory or a tarball: 请注意,您无需将脚本发布到npm就可以安装它-尽管可以执行,也可以直接从GitHub安装项目(包括私有存储库)-但您也可以安装自己的模块在您的本地目录或tarball中:
npm install -g module-on-npm
npm install -g user/repo-on-github
npm install -g /your/local/directory
npm install -g /your/local/tarball.tgz
For more options, see: 有关更多选项,请参见:
Also keep in mind that for your program to be able to be executed from anywhere, you need to use paths relative to __dirname
or __filename
if you need to access your own files relative to your code. 还请记住,要使程序能够从任何地方执行,如果需要访问相对于代码的自己的文件,则需要使用相对于__dirname
或__filename
路径。 See: 看到:
#!/usr/bin/env node
). 在脚本顶部放置一个shebang行(例如#!/usr/bin/env node
)。 $PATH
将脚本放在$PATH
中的目录中 chmod +x /usr/local/bin/example.js
) 赋予它可执行权限(例如chmod +x /usr/local/bin/example.js
) First option: 第一种选择:
You can run your file globally by putting on the first line of the file : #!/usr/bin/env node
, copying it to /usr/local/bin
and make it executable: sudo chmod +x /usr/local/bin/yourfile.js
and then you can call it from where you want with yourfile.js
您可以通过在文件的第一行放上全局代码来运行文件: #!/usr/bin/env node
,将其复制到/usr/local/bin
并使其可执行: sudo chmod +x /usr/local/bin/yourfile.js
,然后您可以使用yourfile.js
从所需位置调用它
Second option: 第二种选择:
Make your local file executable and create an executable bash script which calls your local file and put it in /usr/local/bin
and then call the bashfile globally. 使本地文件可执行,并创建可执行的bash脚本,该脚本将调用本地文件并将其放入/usr/local/bin
,然后全局调用bashfile。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.