Explain Codes LogoExplain Codes Logo

Sending command line arguments to npm script

javascript
prompt-engineering
functions
callbacks
Anton ShumikhinbyAnton Shumikhin·Sep 21, 2024
TLDR

To pass arguments to an npm script, use the -- syntax, and retrieve them in your Node.js script with process.argv or the minimist package.

Usage:

npm run script-name -- --user=Tom --role=admin

Retrieve in Node.js:

const args = require('minimist')(process.argv.slice(2)); console.log(args.user); // Tom console.log(args.role); // admin

Understanding the Arguments

Comprehending command-line arguments involves distinguishing the node flags, npm flags, and script arguments respectively.

Node.js - I too get Arguments?

Node.js gives us process.argv to access CLI arguments. Here's a way to parse them like a pro:

const arguments = process.argv.slice(2); console.log(arguments); // ['--user=Tom', '--role=admin']

Parsing Package Power-Up

Utilize yargs or minimist to simplify the argument retrieval:

const yargs = require('yargs')(process.argv.slice(2)).argv; console.log(yargs.user); // Tom console.log(yargs.role); // admin

Command Line Meets Environment Variables

Environment variables add flexibility to npm scripts. They become especially handy while running applications in different environments.

Dynamic Ports? Yes, Please!

Setting the application's port dynamically can be done with environment variables:

"start": "PORT=3000 node app.js" // Starting off in the comfort zone

Override it when you please:

PORT=5000 npm start // Feeling adventurous today

Oh package.json, Hold this for Me

Here's how you can set and use environment variables within package.json itself:

"scripts": { "start": "webpack-dev-server --port $npm_package_config_port" }
npm config set packageName:defaultPort 8080 // New day, New port!

Npm_config, The Bearer

Npm allows passing arguments via environment variables prefixed with npm_config_. Fetch them in your script as follows:

console.log(process.env.npm_config_custom_port);

Invoke your script with the custom port:

npm run start --custom_port=1337 // An elite choice.

Exploring Special Cases

Platform Peace

Windows and Unix-based systems handle environment variables differently. cross-env is your platform-agnostic friend:

npm install --save-dev cross-env // Installing peace

Update your scripts:

"scripts": { "start": "cross-env PORT=3000 node app.js" }

Naming Etiquettes: Hyphens to Underscores

When working with npm_config_ variables, remember to replace hyphens with underscores in variable names:

npm run start --custom_port=1337 // Correct npm run start --custom-port=1337 // Won't work

Debugging and Best Practices

When things don't act as they should, console.log process.argv. This will allow you to see what arguments Node.js is receiving.

Remember the -- convention for consistency across different tools and environments.

Think twice before passing sensitive data as arguments. These could be exposed in system-wide process listings – a security consideration to not overlook.