CLI with nodejs

北战南征 提交于 2019-12-07 16:25:02

问题


I'm developing a CLI in node that will be published to NPM. Since it's a CLI application, I want it to be included in the path once it's installed, so it's not require to type "node my-app.js" to run it. I want it to run with only "my-app".

In the package.json, I'm including:

"bin": { 
    "my-all" : "./my-app.js" 
 },

But this makes fail the installation via NPM with this error

Error: ENOENT, chmod '/home/user1/node_modules/my-app/my-app'


回答1:


Assuming you're on some kind of unix (linux, osx), put this line at the top of your script:

#!/usr/bin/env node

Also make sure you set the file to executable (chmod a+x my-all).

That should take care of the need to type node my-app.js, and enable you instead to just type ./my-app.js.

As for the npm packaging stuff I am not sure why it fails, but I'm guessing it's an issue with the path or location of your my-app.js .

If an executable script is put anywhere in the PATH, then it will be run just like anything else. If you run which npm, you will see where the npm executable script is located. On my system, most node executable (or executable npm scripts) goes into /usr/local/bin. I'm assuming your package.json can be set to put it somewhere in the path. If you need to change the path, then modify your .profile, or alternatively your system path.



来源:https://stackoverflow.com/questions/11596420/cli-with-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!