Run Node Package + Arguments from another script

I found myself in a situation where I need to run one command, for example. node compile.js

so the .js file should run the following

browserify -t jadeify client/app.js -o bundle.js

All dependencies are installed, and when you run this command in the CLI, everything is fine, you just need to figure out how to execute it from the node script.

We also got the following inside our package.json that contains something similar to

"script" : [ "compile": "browserify -t jadeify client/app.js -o bundle.js" ] this works fine when you execute cd /project && npm run compile through ssh but not through exec

thanks

+6
source share
2 answers

You can access script arguments with process.argv .

An array containing command line arguments. The first element will be "node", the second element will be the name of the JavaScript file. The following items will be any additional command line arguments.

Then you can use the browser api along with jadeify to get what you need.

 var browserify = require('browserify')(); var fs = require('fs'); var lang = process.argv[2]; console.log('Doing something with the lang value: ', lang); browserify.add('./client/app.js'); browserify.transform(require("jadeify")); browserify.bundle().pipe(fs.createWriteStream(__dirname + '/bundle.js')); 

Run it with $ node compile.js enGB

+1
source

You should use api-example and extend it with a transform as suggested by jadeify setup .

 var browserify = require('browserify'); var fs = require('fs'); var b = browserify(); b.add('./client/app.js'); // from jadeify docs b.transform(require("jadeify")); // simple demo outputs to stdout, this writes to a file just like your command line example. b.bundle().pipe(fs.createWriteStream(__dirname + '/bundle.js')); 
+2
source

All Articles