Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you run a js file using npm scripts?

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.

like image 929
Richard Avatar asked Oct 06 '15 08:10

Richard


People also ask

How do I run a .js file?

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.

How do I run a script after npm install?

You can easily run scripts using npm by adding them to the "scripts" field in package. json and run them with npm run <script-name> . Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH , so you can run them by name instead of pointing to node_modules/.


2 Answers

{ "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 
like image 100
Luca Filosofi Avatar answered Oct 09 '22 04:10

Luca Filosofi


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

like image 20
Shanoor Avatar answered Oct 09 '22 03:10

Shanoor