Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use npm scripts within javascript

I need a complete guide or a good reference material to solve the running module commands within javascript file problem.

Say that I often run:

$ npm run webpack-dev-server --progress --colors -- files

How can I run this within a javascript file and execute with

$ node ./script.js

script.js

var webpackDevServer = require('webpack-dev-server');

// need help here
var result = webpackDevServer.execute({
  progress: true,
  colors: true,
}, files);
like image 252
user2167582 Avatar asked Jul 10 '16 04:07

user2167582


People also ask

Can you use npm in JavaScript?

NPM is the default package manager for node. It is used to install, share, and manage javascript packages in a project.

How do I run a npm script?

To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.

Can I run node js in JavaScript?

You can run your JavaScript file from your terminal only if you have installed Node. Js in your system. Install Node. js from Steps to install Node.

What is npm for JavaScript?

npm (originally short for Node Package Manager) is a package manager for the JavaScript programming language maintained by npm, Inc. npm is the default package manager for the JavaScript runtime environment Node.


1 Answers

Answer

I do something like this for my Webpack bundles. You can simply use child_process.spawn to execute command-line programs and handle the process in a node script.

Here's an example:

var spawn = require('child_process').spawn

// ...

// Notice how your arguments are in an array of strings
var child = spawn('./node_modules/.bin/webpack-dev-server', [
    '--progress',
    '--colors',
    '<YOUR ENTRY FILE>'
]);

child.stdout.on('data', function (data) {
    process.stdout.write(data);
});

child.stderr.on('data', function (data) {
    process.stdout.write(data);
});

child.on('exit', function (data) {
    process.stdout.write('I\'m done!');
});

You can handle all of the events you like. This is a fairly powerful module that allows you to view the process' PID (child.pid) and even kill the process whenever you choose (child.kill()).


Addendum

A neat trick is to throw everything into a Promise. Here's a simplified example of what my version of script.js would look like:

module.exports = function () {
    return new Promise(function (resolve, reject) {
        var child = spawn('./node_modules/.bin/webpack', [
            '-d'
        ]);

        child.stdout.on('data', function (data) {
            process.stdout.write(data);
        });

        child.on('error', function (data) {
            reject('Webpack errored!');
        });

        child.on('exit', function () {
            resolve('Webpack completed successfully');
        });
    });
}

Using this method, you can include your script.js in other files and make this code synchronous in your build system or whatever. The possibilities are endless!


Edit The child_process.exec also lets you execute command-line programs:

var exec = require('child_process').exec

// ...

var child = exec('webpack-dev-server --progress --colors <YOUR ENTRY FILES>',
  function(err, stdout, stderr) {
    if (err) throw err;
    else console.log(stdout);
});
like image 188
Kenny Worden Avatar answered Oct 06 '22 00:10

Kenny Worden