Reputation: 93
How can I write my own terminal commands for an NPM project, which would replace the gulp
command without relying on npm run
?
My project contains the following Gulp and http-server custom commands...
## COMMANDS I CURRENTLY HAVE
| Command | Task |
|-----------------------|-----------------------------|
| npm run boom | "Builds then watches files" |
| npm run boom -- build | "Builds the assets files" |
| npm run launch | "Starts a local server" |
This is because of the scripts
in its package.json
file...
/// package.json
{
"scripts": {
"boom": "gulp",
"launch": "http-server -o"
},
"devDependencies": {
"gulp": "^3.9.0",
"gulp-sass": "^2.1.0",
"http-server": "^0.8.5"
}
}
Here is what I would actually like my custom commands to be...
## COMMANDS I WANT TO HAVE
| Command | Task |
|-------------|-----------------------------|
| boom | "Builds then watches files" |
| boom build | "Builds the assets files" |
| boom launch | "Starts a local server" |
"Can I add a debug script to NPM?" is not the same as what I am asking. I've already defined scripts in my package.json
file, which is how I ended up with my current commands. What I want is to write my own project commands/scripts that do the same things as what I have listed, without having to type npm run
.
Example
npm run boom
commandgulp
commandboom
commandUpvotes: 6
Views: 2266
Reputation: 121
Running npm install
only did not help me. Need to run npm link
which helped me run the CLI command.
Where npm link
creates alias like below in the output:
Example :
$LOCAL_DIR\cli-command -> $LOCAL_DIR\npm\node_modules\$PROJECT\dist\index.js
...
Upvotes: 0
Reputation: 741
I usually do this by writing a small CLI using commander then adding a bin
entry into the package.json
pointing to the CLI file.
{
...
"author": "",
"license": "ISC",
"bin":{
"boom": "boom-cli.js"
...
}
Then running either npm install -g
or npm link -g
from the root of the project.
If I recall correctly, npm link -g
creates a symlink between your package and wherever your globally installed npm packages are kept which is preferable for me since I like to change things frequently.
This is a good resource on the topic.
Upvotes: 3