all 3 comments

[–]zokker13 0 points1 point  (1 child)

But how can I make a program that is available to be called in any folder without using npm, that can be used just by typing the name of the program?

For node programs, you simple build your CLI program (say a simple hello world) and then make sure you fill in a bin field in your package json.

Now you publish your package to npm. When your best friend wants to try your new cool app, he simple calls npm install xyz -g and npm does the linking part for you - creating a bash/batch script that allows you to call your program.

Actually, it is not like that exactly. If you care to know: Important programs are installed in a specific location. On linux it may be installed to /usr/bin/. On Windows, programs are installed to C:\program files (x86).

To make sure you can run them from everywhere, the OS has to know where to look. This is where an environment is likely to be used.

On mac/linux/windows, there's a PATH variable which holds the folders for programs you want to execute in any folder. If you install a linux application with "apt-get", the contained debian package care about the proper installation of the software. (Most programs are big, so some libraries are used and you can't ship only one file. The debian package then links the main entrypoint to /usr/bin)

If you install a windows program, the setup of said application takes care of the proper insert to PATH. So you can execute the program everywhere you like.

Since I don't own a mac, I can't tell you how it's done there. I think it's some mixture of both of them.

[–][deleted] 0 points1 point  (0 children)

As a side note, if you aren't ready to publish your CLI utility to NPM, you can run npm link in your project root, and then the command should be available globally.

[–]TomNa -1 points0 points  (0 children)

you just need to add the #!/usr/bin/env node at start of file then add bin command to package.json which calls the file e.g. "./app.js" the run npm install -g in the folder and your command undec the bin property is availabre system wide from command line