adrianmc adrianmc - 1 year ago 87
Bash Question

How to use an npm package binary without installing it

I'm trying to create a CLI tool (in Node, on NPM) that creates an app using Brunch.

So basically, the expected behaviour is that the user will type this to install my tool:

npm install -g my-cli-tool

And then, to create a new Brunch project inside a sub-directory named
, they will run this in their terminal:

my-cli-tool myApp

Brunch will handle most of the work of creating the app itself.

Just for reference, you can create Brunch projects by installing Brunch globally (
npm install -g brunch
) and then simply typing
brunch new myApp

I've already setup the necessary stuff for making an NPM package that exposes a binary, so I will just reduce it down to my one script.

What I have tried so far is the following:

#! /usr/bin/env node
var shell = require("shelljs");

if (!process.argv[2]) {
console.log('You need to supply a name for your app!');
console.log(' my-cli-tool <project-name>');
} else {
var appName = process.argv[2];
console.log('Your app is now being created...');

console.log('Installing Brunch...');
shell.exec('npm install --save brunch');

console.log('Creating app with Brunch...');
shell.exec('PATH=$(npm bin):$PATH brunch new ' + appName);

console.log('Success! Your app is done.');

It works as expected but the problem is, installing Brunch (
npm install --save brunch
) will create a
folder inside the current working directory along with a

Ideally, I want the current working directory to be untouched except for the creation of a folder named
(which is done by Brunch itself). Essentially, I want to use Brunch to generate the app without having any trace of having installed Brunch itself.

I've taken a look at
npm pack
but I am not sure how to apply it to my use case. Any help would be appreciated, thanks.

Answer Source
  1. It's crazy to install brunch each time you create an App
  2. Nobody needs a wrapper around a wrapper
  3. Solution: Put brunch in the dependencies of your-awsesome-cli-tool and call it from your package directory
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download