npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

npm-commands

v1.2.1

Published

Run npm commands programmatically

Downloads

21,329

Readme

npm-commands

Execute npm commands programmatically in your Node scripts!

Motivation

How many times did you have to execute some npm commands in your Node scripts? I had. A lot.

What you can do

With this library you can execute, both synchronously and asynchronously:

  • npm run <script>
  • npm install
  • npm install <module>
  • npm link
  • npm link <module>

Examples

Let's assume you've got some scripts in you package.json:

"scripts": {
    "build": "node ./scripts/build.js",
    "release": "node ./scripts/release.js",
    "deploy": "node ./scripts/deploy.js"
}

and let's say you need to build and release when you deploy.

For sure you could chain the two scripts and do something like

"scripts": {
    "deploy": "npm run build && npm run release"    
}

That's ok, but you can have some issues because of the different plaforms (Linux, MacOs and Windows) and maybe you have to perform some stuff before you actually deploy; therefore you created your deploy script.

Your deploy script could look like

/* do some stuff
... your fancy code here ../
*/

// we need to run our script, so we need to run a new process
const { execSync } = require('child_process');

// in case you have no params to pass through
execSync('npm run build', { stdio: 'inherit', cwd: 'path/to/dir' });

// but in case you have to also pass some arguments, you would need to change the above into this
const params = process.argv.slice(2);
execSync(`npm run build -- ${params}`, { stdio: 'inherit', cwd: 'path/to/dir' });

/* the rest of your code here */

Pretty ugly isn't it?

Using this library you would do

/* do some stuff
... your fancy code here ../
*/

// we need to run our script, so we need to run a new process
const npm = require('npm-commands');

// arguments are passed through by default
npm().run('build');

// but in case you don't want to pass arguments
npm().arguments(false).run('build')
/* the rest of your code here */

Examples

Changing the working director

npm().cwd('path/to/').run('build');

// install the dependencies from the specified folder 
npm().cwd('path/to/').install();

// install react in the specified folder
npm().cwd('path/to/').install('react');

Suppress the output

// this will show the output in the console
npm().run('build');

// this too
npm().output(true).run('build');

// this won't 
npm().output(false).run('build');

Provide arguments

/* 
 * assuming you have run 
 * $ node scripts/build.js --mode prod
 * this will pass the mode to your script automatically
*/
npm().run('build')

// this will ignore the cli arguments
npm().arguments(false).run('build')

// this will pass specific ones
npm().arguments({ mode: 'dev', runTests: false }).run('build');

.run, .install and .link have also an async version that returns a Promise.

npm().runAsync('build').then(output => {
  // do something with the output  
});

npm().installAsync('react').then(output => {
  // do something with the output  
});

:class

| Method | Description | Params | Returns | | ------ | ----------- | ------ | ------- | | cwd | Changes the working director | directory: <String> | instance | | output | Sets whether to show the command output | value: <Boolean> | instance | | arguments | Remaps the CLI arguments | args: <Object> | instance | | install | Runs npm install | module: <?String> | output: <String> | | link | Runs npm link | module: <?String> | output: <String> | | unlink | Runs npm unlink | module: <?String> | output: <String> | | run | Runs npm run <script> | script: <String> | output: <String> |

TODO

  • tests
  • potentially cover other commands