简体   繁体   English

安装node.js模块以在命令提示符下全局使用

[英]Install node.js module for global use in command prompt

I have node.js module, made with type-script. 我有用type-script制作的node.js模块。 After installing type-script module globally: 全局安装类型脚本模块后:

npm install -g ts-node

I have made a module called e2e , which gets command prompt arguements. 我已经制作了一个名为e2e的模块,该模块可以获取命令提示符。 After that I can call 之后我可以打电话

ts-node e2e

But then, I still need to call this module e2e, via 但是,我仍然需要通过以下方式将此模块称为e2e

e2e -a -b -c

where -a, -b, -c are command prompth params. 其中-a,-b,-c是命令提示符参数。

I tried to execute bat file like: 我试着像这样执行bat文件:

ts-node e2e -a -b -c

but it calls ts-node with this 4 parameters (e2e, -a, -b, -c). 但会使用这4个参数(e2e,-a,-b,-c)调用ts-node。

How this problem could be solved? 这个问题怎么解决? I finaly need to call e2e with params. 我最终需要使用参数来调用e2e。

You need to use a shebang line as the first line of your script. 您需要使用shebang行作为脚本的第一行。

For example: 例如:

#!/usr/bin/env node

See: 看到:

Then you will have it to be installed globally with: 然后,您将使用以下命令将其全局安装:

npm install -g your-module

and for that you need to include the bin portion of package.json. 为此,您需要包括package.json的bin部分。 See the docs: 参见文档:

Or you can just copy it manually to some directory that you have in your PATH, like /usr/bin/local/bin etc. but it needs the shebang line and it has to be executable: 或者,您可以手动将其复制到PATH中的某个目录中,例如/usr/bin/local/bin等。但是它需要使用shebang行并且必须是可执行的:

chmod a+x your-file-name

See: 看到:

Example

For example on how to do it see my project on GitHub and npm that does exactly that: 例如,有关如何执行此操作的示例,请参见我在GitHub和npm上的项目,它确实做到了:

Shebang line 社bang线

See the shebang line in the script: 参见脚本中的shebang行:

 #!/usr/bin/env node 

The package.json package.json

See the "bin" portion in package.json : 请参阅package.json"bin"部分:

 "bin": { "websocket-vs-socket.io": "ws-vs-si.js" }, 

As you can see, you can even install it under a different name than the original file was named. 如您所见,您甚至可以使用与原始文件不同的名称来安装它。

Usage 用法

And see the the instruction on how to install it and use it: 并查看有关如何安装和使用它的说明:

Install: 安装:

 npm i -g websocket-vs-socket.io 

Run the server: 运行服务器:

 websocket-vs-socket.io 

This is a pretty simple project that you can see at: 这是一个非常简单的项目,您可以在以下位置看到:

and: 和:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM