I want to be able to execute the command script1
in a project directory that will run node script1.js
.
script1.js
is a file in the same directory. The command needs to be specific to the project directory, meaning that if I send someone else the project folder, they will be able to run the same command.
So far I've tried adding:
"scripts": {
"script1": "node script1.js"
}
to my package.json file but when I try running script1
I get the following output:
zsh: command not found: script1
Does anyone know the steps necessary to add the script mentioned above to the project folder?
*Note: the command can not be added to the bash profile (cannot be a machine specific command)
Please let me know if you need any clarification.
Custom Scripts
npm run-script <custom_script_name>
or
npm run <custom_script_name>
In your example, you would want to run
npm run-script script1
ornpm run script1
.See https://docs.npmjs.com/cli/run-script
Lifecycle Scripts
Node also allows you to run custom scripts for certain lifecycle events, like after
npm install
is run. These can be found here.For example:
This would run
electron-rebuild
after anpm install
command.Steps are below:
In package.json add:
Create a
bin
folder in the project directory and add filerunScript1.js
with the code:Run
npm install shelljs
in terminalRun
npm link
in terminalFrom terminal you can now run
script1
which will runnode script1.js
Reference: http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm
Example:
As you can see, the script "build_c" is building the angular application, then deletes all old files from a directory, then finally copies the result build files.
I have created the following, and it's working on my system. Please try this:
package.json:
script1.js:
From your command line run the following command:
Additional use case
My package.json file has generally the following scripts, which enable me to watch my files for typescript, sass compilations and running a server as well.