如何运行节点。来自另一个节点的js脚本。js脚本

时间:2021-11-13 14:53:56

I have a standalone Node script called compile.js. It is sitting inside the main folder of a small Express app.

我有一个名为compip .js的独立节点脚本。它位于一个小型快递应用的主文件夹中。

Sometimes I will run the compile.js script from the command line. In other scenarios, I want it to be executed by the Express app.

有时我会运行编译。来自命令行的js脚本。在其他场景中,我希望它由Express应用程序执行。

Both scripts load config data from the package.json. Compile.js does not export any methods at this time.

这两个脚本都从package.json加载配置数据。编译。此时js不导出任何方法。

What is the best way to load up this file and execute it? I have looked at eval(), vm.RunInNewContext, and require, but not sure what is the right approach.

加载并执行这个文件的最佳方式是什么?我查看了eval(), vm。RunInNewContext,并要求,但不确定什么是正确的方法。

Thanks for any help!!

感谢任何帮助! !

2 个解决方案

#1


32  

You can use a child process to run the script, and listen for exit and error events to know when the process is completed or errors out (which in some cases may result in the exit event not firing). This method has the advantage of working with any async script, even those that are not explicitly designed to be run as a child process, such as a third party script you would like to invoke. Example:

您可以使用子进程来运行脚本,并侦听退出和错误事件,以了解进程何时完成或错误(在某些情况下可能导致退出事件而不是触发)。这个方法的优点是可以使用任何异步脚本,即使那些不是显式设计为作为子进程运行的脚本,比如您希望调用的第三方脚本。例子:

var childProcess = require('child_process');

function runScript(scriptPath, callback) {

    // keep track of whether callback has been invoked to prevent multiple invocations
    var invoked = false;

    var process = childProcess.fork(scriptPath);

    // listen for errors as they may prevent the exit event from firing
    process.on('error', function (err) {
        if (invoked) return;
        invoked = true;
        callback(err);
    });

    // execute the callback once the process has finished running
    process.on('exit', function (code) {
        if (invoked) return;
        invoked = true;
        var err = code === 0 ? null : new Error('exit code ' + code);
        callback(err);
    });

}

// Now we can run a script and invoke a callback when complete, e.g.
runScript('./some-script.js', function (err) {
    if (err) throw err;
    console.log('finished running some-script.js');
});

Note that if running third-party scripts in an environment where security issues may exist, it may be preferable to run the script in a sandboxed vm context.

请注意,如果在安全问题可能存在的环境中运行第三方脚本,那么最好在sandboxed虚拟机环境中运行脚本。

#2


1  

Forking a child process may be useful, see http://nodejs.org/api/child_process.html

分出子进程可能是有用的,请参见http://nodejs.org/api/child_process.html

From the example at link:

从链接的例子来看:

var cp = require('child_process');

var n = cp.fork(__dirname + '/sub.js');

n.on('message', function(m) {
  console.log('PARENT got message:', m);
});

n.send({ hello: 'world' });

Now, the child process would go like... also from the example:

现在,孩子的过程就像…同样的例子:

process.on('message', function(m) {
  console.log('CHILD got message:', m);
});

process.send({ foo: 'bar' });

But to do simple tasks I think that creating a module that extends the events.EventEmitter class will do... http://nodejs.org/api/events.html

但是要完成简单的任务,我认为创建一个扩展事件的模块。EventEmitter类将做……http://nodejs.org/api/events.html

#1


32  

You can use a child process to run the script, and listen for exit and error events to know when the process is completed or errors out (which in some cases may result in the exit event not firing). This method has the advantage of working with any async script, even those that are not explicitly designed to be run as a child process, such as a third party script you would like to invoke. Example:

您可以使用子进程来运行脚本,并侦听退出和错误事件,以了解进程何时完成或错误(在某些情况下可能导致退出事件而不是触发)。这个方法的优点是可以使用任何异步脚本,即使那些不是显式设计为作为子进程运行的脚本,比如您希望调用的第三方脚本。例子:

var childProcess = require('child_process');

function runScript(scriptPath, callback) {

    // keep track of whether callback has been invoked to prevent multiple invocations
    var invoked = false;

    var process = childProcess.fork(scriptPath);

    // listen for errors as they may prevent the exit event from firing
    process.on('error', function (err) {
        if (invoked) return;
        invoked = true;
        callback(err);
    });

    // execute the callback once the process has finished running
    process.on('exit', function (code) {
        if (invoked) return;
        invoked = true;
        var err = code === 0 ? null : new Error('exit code ' + code);
        callback(err);
    });

}

// Now we can run a script and invoke a callback when complete, e.g.
runScript('./some-script.js', function (err) {
    if (err) throw err;
    console.log('finished running some-script.js');
});

Note that if running third-party scripts in an environment where security issues may exist, it may be preferable to run the script in a sandboxed vm context.

请注意,如果在安全问题可能存在的环境中运行第三方脚本,那么最好在sandboxed虚拟机环境中运行脚本。

#2


1  

Forking a child process may be useful, see http://nodejs.org/api/child_process.html

分出子进程可能是有用的,请参见http://nodejs.org/api/child_process.html

From the example at link:

从链接的例子来看:

var cp = require('child_process');

var n = cp.fork(__dirname + '/sub.js');

n.on('message', function(m) {
  console.log('PARENT got message:', m);
});

n.send({ hello: 'world' });

Now, the child process would go like... also from the example:

现在,孩子的过程就像…同样的例子:

process.on('message', function(m) {
  console.log('CHILD got message:', m);
});

process.send({ foo: 'bar' });

But to do simple tasks I think that creating a module that extends the events.EventEmitter class will do... http://nodejs.org/api/events.html

但是要完成简单的任务,我认为创建一个扩展事件的模块。EventEmitter类将做……http://nodejs.org/api/events.html