简体   繁体   中英

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. Instead of typing node fileName.js , I need to be able to just type newFile to run it. 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. But I would like to avoid using a Bash script if I can.

I looked into Node to see if I can do this. All I found was information about putting the name of the program in the package.json file, as a value for "start", under "scripts." But then to run the program I would have to type 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.

Is there a way to do what I want, with or without Node, without using an external script?

No you can't... It might possible with shell script. But in your machine node js run with help of npm.

Every javascript project has a package.json file which is managed package file for your project. So you can not do what you want.

What I suggest: Use nodemon.

npm i nodemon

then in package.json add another script "dev" or whatever you want or you can write under start script.

"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; dont forge to press rs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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