How to pack Node.JS project into a tarball?

Today I needed to install my brand new NodeJS-developed CLI application in my computer.

Important! Don’t forget to add this to your package.json file:

  "bin": {
    "skynet": "./bin/skynetApp.js"

It’s basically the name of the command and the NodeJS file it will execute, in this case the command name is skynet and every time this is typed in a terminal window it will execute the file ./bin/skynetApp.js. If you don’t do this you will never get to run your program from the terminal.

After a quick search on the Internet I became aware that NPM allows me to do that very easily. The command is:

npm pack

Run it inside the project that you want to pack (at the same level of the package.json file).

Here is an example of how your console output should look:

Z:\Code\NodeJS\skynetProject>npm pack
npm notice
npm notice package: skynetProject@1.0.0
npm notice === Tarball Contents ===
npm notice 12.3kB bin/cfc.js
npm notice 72.6kB data/skynetSecrets.json
npm notice 396B package.json
npm notice === Tarball Details ===
npm notice name: skynetProject
npm notice version: 1.0.0
npm notice filename: skynetProject-1.0.0.tgz
npm notice package size: 6.6 kB
npm notice unpacked size: 85.3 kB
npm notice shasum: 29807f45119eacf60a5ea54f44460e9327cca930
npm notice integrity: sha512-RkrYzmyVLr2fp[...]6CuHh+bGh7YHw==
npm notice total files: 3
npm notice

Now, onto the fun part! Install my tarball globally so I can run it from anywhere in the command line:

Z:\Code\NodeJS\skynetProject>npm install -g skynetProject-1.0.0.tgz
C:\Users\Terminator-T800\AppData\Roaming\npm\cfc -> C:\Users\Terminator-T800\AppData\Roaming\npm\node_modules\skynetProject\bin\cfc.js
+ skynetProject@1.0.0
added 29 packages from 13 contributors in 4.22s

Boom! it’s done. You can now open a terminal window and type the command skynet to run the program.

If for some reason you want to uninstall it you can quickly do it with the following command (notice that in this case I indicate the name of the project):

Z:\Code\NodeJS\skynetProject>npm uninstall -g skynetProject
removed 29 packages in 2.651s