简体   繁体   English

使用别名调用 JavaScript 或 Node.js 程序

[英]Invoke a JavaScript or Node.js program using an alias

I am writing a JavaScript program that needs to invoked on the command line using a special command/alias.我正在编写一个需要使用特殊命令/别名在命令行上调用的 JavaScript 程序。 Instead of typing node fileName.js , I need to be able to just type newFile to run it.而不是键入node fileName.js ,我需要能够只键入newFile来运行它。 My understanding (correct me if I'm wrong) is that I could create a Bash script to run the JavaScript and use an alias to run that.我的理解(如果我错了请纠正我)是我可以创建一个 Bash 脚本来运行 JavaScript 并使用别名来运行它。 But I would like to avoid using a Bash script if I can.但是如果可以的话,我想避免使用 Bash 脚本。

I looked into Node to see if I can do this.我查看了 Node,看看我是否可以做到这一点。 All I found was information about putting the name of the program in the package.json file, as a value for "start", under "scripts."我发现的只是关于将程序名称放在 package.json 文件中的信息,作为“start”的值,在“scripts”下。 But then to run the program I would have to type npm start .但是要运行该程序,我必须键入npm start I looked for a way that I could create an alias for npm start within Node itself, but I didn't find one.我寻找一种可以在 Node 本身内为npm start创建别名的方法,但我没有找到。

Is there a way to do what I want, with or without Node, without using an external script?有没有办法在不使用外部脚本的情况下使用或不使用 Node 来做我想做的事?

No you can't... It might possible with shell script.不,你不能......用shell脚本可能。 But in your machine node js run with help of npm.但是在你的机器节点 js 的帮助下运行 npm。

Every javascript project has a package.json file which is managed package file for your project.每个 javascript 项目都有一个 package.json 文件,它是您项目的托管包文件。 So you can not do what you want.所以你不能做你想做的。

What I suggest: Use nodemon.我的建议:使用 nodemon。

npm i nodemon

then in package.json add another script "dev" or whatever you want or you can write under start script.然后在 package.json 中添加另一个脚本“dev”或任何你想要的,或者你可以在启动脚本下编写。

"dev":"nodemon app.js",

Then everytime you save your application your server will restart automatically然后每次保存应用程序时,您的服务器都会自动重新启动

Note: Each time you copy or select something from command prompt while using nodemon;注意:每次在使用 nodemon 时从命令提示符复制或选择内容时; dont forge to press rs不要伪造按rs

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

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