Software Development

How to create and run a custom NPM script in package.json to execute a JavaScript file?

RY Asked by Ryan Miller · 22-03-2025
0 upvotes 14,456 views 0 comments
The question

I am working on a Software Development project using Node.js and I want to automate a specific task by running a local script called "utility.js". Instead of typing node utility.js every time, I want to integrate this into my package.json file. How do I define a custom command in the scripts section, and what is the correct terminal syntax to trigger it? I also need to know if I can pass arguments through NPM to my underlying JavaScript file.

3 answers

0
KI
Answered on 23-03-2025

Adding a custom script is straightforward. Open your package.json and locate the "scripts" object. You can add a new key-value pair where the key is your command name and the value is the execution string. For example: "my-task": "node utility.js". To run this, you would use npm run my-task in your terminal. This is widely used in Software Development to wrap complex commands—like starting a linter or a test suite—into a single, easy-to-remember word. If your file is in a subfolder, just ensure the path is correct, such as "node ./scripts/utility.js".

0
ST
Answered on 25-03-2025

Does the npm run command automatically pick up environment variables defined in a .env file, or do I need to use a package like dotenv within my "utility.js" file to access them?

RY 26-03-2025

Steven, NPM doesn't load .env files by default. You have two common options in Software Development: either use the dotenv package at the top of your "utility.js" or use a tool like dotenv-cli in your script definition, like "my-task": "dotenv -e .env node utility.js". Personally, I prefer importing dotenv directly in the code so the script remains portable regardless of how it's called. This ensures your configuration is consistent across different deployment environments.

0
MA
Answered on 28-03-2025

If you need to pass arguments, remember to use the double dash. For example: npm run my-task -- --port 3000. This tells NPM to pass everything after the -- directly to your Node process.

KI 29-03-2025

I agree with Margaret. The double dash is a lifesaver when you're building dynamic Software Development tools where you need to change parameters on the fly without editing the package.json file every time.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session