How do you run a js file using npm scripts?

Javascriptnode.jsNpm

Javascript Problem Overview


I can't get npm to work. My package.json file has

"scripts": { "build": "build.js" }

and I have a build.js file in the same folder that just console.logs.

When I run

npm run build

I get the error

The system cannot execute the specified program.

npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v4.1.1
npm ERR! npm  v3.3.5
npm ERR! code ELIFECYCLE

and if I move the build.js file and change my package.json file to have a subfolder

"scripts": { "build": "build/build.js" }

then I get the error

'build' is not recognized as an internal or external command, operable program or batch file.

What's going wrong? I'm copying the example documentation.

Javascript Solutions


Solution 1 - Javascript

{ "scripts" :
  { "build": "node build.js"}
}

> npm run build OR npm run-script build


{
  "name": "build",
  "version": "1.0.0",
  "scripts": {
    "start": "node build.js"
  }
}

> npm start


> NB: you were missing the { brackets } and the node command

folder structure is fine:

+ build
  - package.json
  - build.js

Solution 2 - Javascript

You should use npm run-script build or npm build <project_folder>. More info here: https://docs.npmjs.com/cli/build.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichardView Question on Stackoverflow
Solution 1 - JavascriptLuca FilosofiView Answer on Stackoverflow
Solution 2 - JavascriptShanoorView Answer on Stackoverflow