How to run node script from BASH?


Below shell script is running a node script, which gets an object from package.json, updates the app version and writes it back to the package.json using node API.

// foo.sh

node <<EOF

var fs = require("fs");
var pkgJSON = require("./package.json");

pkgJSON.app.version = "12.32.5";

var p = JSON.stringify(pkgJSON);
fs.writeFile("./package.json", p, "utf8", (err) => {
  if (err)
    throw err;
  console.log('The updated app version has been saved!');
});

EOF

You can run the above shell script by typing /foo.sh from command line. Make sure that script is executable. If not then change its permission using chmod 777 ./foo.sh

Also, Checkout how-to-run-unix-commands-from-nodejs


You may also like...