admin管理员组

文章数量:1129092

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.

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.

Share Improve this question edited Jul 4, 2024 at 14:41 Rob Bednark 28.1k26 gold badges87 silver badges128 bronze badges asked Apr 5, 2016 at 17:45 Jake.JSJake.JS 3,4363 gold badges16 silver badges20 bronze badges 4
  • 8 how are you trying to run it? are you using "npm run script1"? – niorad Commented Apr 5, 2016 at 17:49
  • 7 did you run script1 using npm run script1? – Claudiordgz Commented Apr 5, 2016 at 17:49
  • 1 @Claudiordgz is right, or as in Sujeet's answer, "npm start" and "npm test" are shortcuts for scripts called "start" and "test" – mejdev Commented Apr 5, 2016 at 18:09
  • 1 in my terminal I need to be able to type the one word command called script1 which should run node script1.js – Jake.JS Commented Apr 5, 2016 at 18:25
Add a comment  | 

8 Answers 8

Reset to default 378

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.

This works for me:

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" 
  }

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

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.

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

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.

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.

Update as of 2024

The release of Node.js 22 this year has shipped with a newer and native alternative to the npm run.

To execute custom scripts, you use the node --run:

node --run <custom_script_name> 

So you can have the package.json file like this with the start script:

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  }
}

Assuming your index.js file contains the following:

console.log('Program runs')

In the terminal, you can run the start script in the package.json with the node --run command like this:

node --run start

The output will look like the following:

Program runs
(node:22407) ExperimentalWarning: Task runner is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

Because its experimental you get a warning.

本文标签: nodejsHow to add a custom script to packagejson that runs a javascript fileStack Overflow