Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

javascript - How can I create CLI app such as ng which is installed globally into the system?

When I install

npm i ng -g

how does it understand the path for installation?

Actually I want to create the app which is installing and working the similar way

question from:https://stackoverflow.com/questions/65876784/how-can-i-create-cli-app-such-as-ng-which-is-installed-globally-into-the-system

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you run npm install --global, this is usually (not always) for packages that expose a CLI. If this is the case you can use the handy UNIX which command to find out where it is.

For example, by default when you install NodeJs, npm is intsalled as a global package. Therefore you are virtually guaranteed to find it, like so:

$ which npm 
/home/bguiz/.nvm/versions/node/v12.16.1/bin/npm

Your path will depend on a number of factors, such as your operating system, which method you used to install node, currently set environment variables, symlinks, etc. Whatever it is, ls that path without the bin/npm at the end.

$ ls /home/bguiz/.nvm/versions/node/v12.16.1/
bin  CHANGELOG.md  include  lib  LICENSE  README.md  share

This is the "global" node directory.

Back to your original question, about where ng is installed. Add node_modules/${PACKAGE_NAME} to the global node directory, and ls that. In my example this would be:

$ ls /home/bguiz/.nvm/versions/node/v12.16.1/lib/node_modules/ng
ls: cannot access '/home/bguiz/.nvm/versions/node/v12.16.1/lib/node_modules/ng': No such file or directory

I do not have ng installed globally, so the above ls command fails. But in your case, you should see the contents of the ng package installation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...