259

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.

hong4rc
  • 3,999
  • 4
  • 21
  • 40
Jake.JS
  • 3,266
  • 3
  • 15
  • 18

7 Answers7

370

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 or npm 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:

"scripts": {
    "postinstall": "electron-rebuild",
},

This would run electron-rebuild after a npm install command.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
wesleysmyth
  • 4,348
  • 2
  • 17
  • 17
  • 1
    `npm run-script scriptname` worked for me, however `npm run scriptname` didn't! – blueprintchris Jul 31 '18 at 12:52
  • How it possible to run a custom script without the "run" command? Sails.js dose that - it has a command `sails lift` which does even requiring NPM. Does installing it via NPM add a terminal script on the system where it's installed? If not, how is this made? – Gal Grünfeld Mar 24 '19 at 19:17
  • @GalGrünfeld did you install sails globally (npm install -g)? – Yves Dorfsman Apr 10 '19 at 14:54
  • I have, and I did some reading, aand as far as can remember from Sail's website, installing it globally (via `-g`), found out that Sails installs bash/cmdlet scripts (e.g `sails generate api ` (bash/cmdlet according to the machine it's installs on) and adds global paths to those scripts on the machine. – Gal Grünfeld May 09 '19 at 00:12
  • how to run js file of downloaded npm package through my package.json? – Hardik Rana May 09 '20 at 16:29
46

I have created the following, and it's working on my system. Please try this:

package.json:

{
  "name": "test app",
  "version": "1.0.0",
  "scripts": {
    "start": "node script1.js"   
  }
}

script1.js:

console.log('testing')

From your command line run the following command:

npm start

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.

 "scripts": {
    "start": "concurrently \"sass --watch ./style/sass:./style/css\" \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w", 
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 
  }
ruffin
  • 16,507
  • 9
  • 88
  • 138
Sujeet Jaiswal
  • 1,071
  • 7
  • 12
  • 1
    the command to run the ```script1.js``` file needs to be a custom one word command called ```script1``` – Jake.JS Apr 05 '16 at 18:20
  • 1
    then change `"start"` to `script1`, you can use any name you like, I prefer to use `start` to clearly defined what should run – Sujeet Jaiswal Apr 05 '16 at 18:29
  • 4
    @sujeet-jaiswal simply changing from start to script1 is not going to work. The word "start" is reserved in npm, so it works. The word script1 is not and it will not be recognized, even when it is defined in package.json The wesleysmyth answer above is the correct one, just add run to the call. – Predrag Stojadinović Nov 27 '17 at 10:25
  • No. @SujeetJaiswal is correct. It's in NPM's docs: https://docs.npmjs.com/cli/v8/commands/npm-start – JonTroncoso Jun 28 '22 at 05:42
36

Steps are below:

  1. In package.json add:

    "bin":{
        "script1": "bin/script1.js" 
    }
    
  2. Create a bin folder in the project directory and add file runScript1.js with the code:

    #! /usr/bin/env node
    var shell = require("shelljs");
    shell.exec("node step1script.js");
    
  3. Run npm install shelljs in terminal

  4. Run npm link in terminal

  5. From terminal you can now run script1 which will run node script1.js

Reference: http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm

Bryan
  • 2,870
  • 24
  • 39
  • 44
Jake.JS
  • 3,266
  • 3
  • 15
  • 18
12

Lets say in scripts you want to run 2 commands with a single command:

"scripts":{
  "start":"any command",
  "singleCommandToRunTwoCommand":"some command here && npm start"
}

Now go to your terminal and run there npm run singleCommandToRunTwoCommand.

kwoxer
  • 3,734
  • 4
  • 40
  • 70
Soban Arshad
  • 1,295
  • 19
  • 15
  • Whilst helpful, this doesn't directly answer the OP's question. – AdamJB Jan 08 '20 at 11:12
  • 1
    Basically we are intended to guide onto the rigth path, more over you do on your own. This is the proper way of learning. – Soban Arshad Jan 09 '20 at 12:11
  • @AdamJB He does, because he used **npm run ** – Kubadev Feb 28 '20 at 11:10
  • @kubadev, I don't mean to be pedantic, was just pointing out that the OP requires the script to be run with a one word command as per his own comment on his question: `In my terminal I need to be able to type the one word command called script1 which should run node script1.js` – AdamJB Mar 02 '20 at 08:32
9

Suppose I have this line of scripts in my "package.json"

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "export_advertisements": "node export.js advertisements",
    "export_homedata": "node export.js homedata",
    "export_customdata": "node export.js customdata",
    "export_rooms": "node export.js rooms"
  },

Now to run the script "export_advertisements", I will simply go to the terminal and type

npm run export_advertisements
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Raj Kanchan
  • 450
  • 6
  • 11
1

Example:

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "build_c": "ng build --prod && del \"../../server/front-end/*.*\" /s /q & xcopy /s dist \"../../server/front-end\"",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

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.

FindOutIslamNow
  • 1,169
  • 1
  • 14
  • 33
  • 2
    Here's some useful info about `&&` - https://stackoverflow.com/questions/39172536/running-npm-scripts-sequentially?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Simon_Weaver May 09 '18 at 20:53
1

In my case, i was too stupid and I was running the below command

node run build

rather than below command

npm run build

please recheck your command once before cleaning and rerunning install.

Also please don't forget about npx which it you can use any module without installing it.

Shivang Agarwal
  • 1,825
  • 1
  • 14
  • 19