Hello Everyone,
I have been trying to learn how to install my *nix command projects on my computer so I can use them. I just did so with my own cat.js
project so I’ll use that as an example.
- Your script needs to be in its own directory, with a package.json. All of you packages should be installed.
catjs/
| _ node_modules
| _ package.json
| _ package-lock.json
| _ cat.js
- My package.json was set up like the following:
{
"name": "catjs",
"version": "1.0.0",
"description": "javascript implementation of 'cat' bash command",
"main": "cat.js",
"dependencies": {
"command-line-args": "^5.0.2",
"command-line-usage": "^5.0.5"
},
"bin": {
"catjs": "./cat.js"
},
"author": "Zach Berwaldt",
"license": "ISC"
}
the "bin"
field is so the catjs
command maps to my script.
- At the top of your script. In my case `cat.js’ you put the following:
#!/usr/bin/env node
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');
const fs = require('fs');
const optionDefinitions = [
{ name: 'help', alias: 'h', type: Boolean, description: '' },
...
The #!/usr/bin/env node
is called a shebang. Basically it makes the file be read as an executable.
- Finally install your command:
npm install -g
. Do the previous command while in the directory of your project or if your outside:npm install -g ./your-dir
.
I am on Windows 10, and the above worked for me.
I also read about: npm link
which supposedly does the same as npm install
but you can continue to develop your cli and you won’t have to reinstall. I haven’t tried it myself so I can’t vouch for it.
Here is the documentation for that: https://docs.npmjs.com/cli/link