How do I add a custom script to my package json file that runs a javascript file

0 votes

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?

Jul 16, 2020 in Node-js by kartik
• 37,520 points
41,855 views

1 answer to this question.

0 votes

Hello @kartik,

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

Hope it helps!!

And to know more about javascript file, join our Java certification course and learn Java online.
Thanks!!

answered Jul 16, 2020 by Niroj
• 82,840 points
Is there any way to add it other than doing it manually??

Hello @P. Muthukumar,

Try this:

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

0 votes
Try with this, Apart from predefined command like (Start, etc..) if you trying to add any custom command so you hav to must append "run" keyword
npm run script1
answered Oct 29, 2022 by Pritam

edited 5 days ago
0 votes

run npm run script1 it works for me

answered Jan 10, 2023 by Harisudarsan

edited 5 days ago

Related Questions In Node-js

0 votes
1 answer

How do I add custom JS to react?

Creating custom objects in React involves defining ...READ MORE

answered Jan 10 in Node-js by Navya
93 views
0 votes
1 answer
0 votes
1 answer

How to clean node_modules folder of packages that are not in package.json?

Hello @kartik, You could remove your node_modules/ folder ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
30,716 views
0 votes
1 answer

How do I uninstall a package installed using npm link?

Hello @kartik, The package can be uninstalled using ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
4,647 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
1,716 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
1,339 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
1,196 views
0 votes
1 answer

How to change Laravel official name to any customize name?

Hey, You just need to go Laravel folder through ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
3,265 views
0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,086 views
0 votes
1 answer

How do I send command line arguments to npm script?

Hello @kartik, The syntax is as follows: npm run ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
11,175 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP