Javascript 执行一些代码然后进入交互节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8549145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:28:02  来源:igfitidea点击:

execute some code and then go into interactive node

javascriptnode.js

提问by slacktracer

Is there a way to execute some code (in a file or from a string, doesn't really matter) before dropping into interactive mode in node.js?

有没有办法在 node.js 中进入交互模式之前执行一些代码(在文件中或从字符串中,并不重要)?

For example, if I create a script __preamble__.jswhich contains:

例如,如果我创建一个__preamble__.js包含以下内容的脚本:

console.log("preamble executed! poor guy!");

and a user types node __preamble__.jsthey get this output:

并且用户输入node __preamble__.js他们得到这个输出:

preamble executed! poor guy!
> [interactive mode]

回答by slacktracer

Really old question but...

真的很老的问题,但是...

I was looking for something similar, I believe, and found out this. You can open the REPL (typing nodeon your terminal) and then load a file. Like this: .load ./script.js. Press enter and the file content will be executed. Now everything created (object, variable, function) in your script will be available.

我一直在寻找类似的东西,我相信,并发现了这一点。您可以打开 REPL(node在终端上输入),然后加载文件。像这样:.load ./script.js。按回车键,文件内容将被执行。现在脚本中创建的所有内容(对象、变量、函数)都将可用。

For example:

例如:

// script.js
var y = {
    name: 'obj',
    status: true
};

var x = setInterval(function () {
    console.log('As time goes by...');
}, 5000);

On the REPL:

在 REPL 上:

//REPL
.load ./script.js

Now you type on the REPL and interact with the "living code". You can console.log(y)or clearInterval(x);

现在您输入 REPL 并与“活代码”交互。你可以console.log(y)clearInterval(x);

It will be a bit odd, cause "As time goes by..." keep showing up every five seconds (or so). But it will work!

这会有点奇怪,因为“随着时间的流逝......”每隔五秒(左右)就会出现一次。但它会起作用!

回答by sarnold

You can start a new replin your Node software pretty easily:

你可以repl很容易地在你的 Node 软件中开始一个新的:

var repl = require("repl");
var r = repl.start("node> ");
r.context.pause = pauseHTTP;
r.context.resume = resumeHTTP;

From within the REPL you can then call pause()or resume()and execute the functions pauseHTTP()and resumeHTTP()directly. Just assign whatever you want to expose to the REPL's contextmember.

从REPL内你就可以打电话pause()resume()和执行功能pauseHTTP()resumeHTTP()直接。只需将您想要公开的内容分配给 REPL 的context成员。

回答by gnerkus

This can be achieved with the current version of NodeJS(5.9.1):

这可以通过当前版本的 NodeJS( 5.9.1) 实现:

$ node -i -e "console.log('A message')"

The -eflag evaluates the string and the -iflag begins the interactive mode.

-e标志评估字符串和-i标志开始的交互模式。

You can read more in the referenced pull request

您可以在引用的拉取请求中阅读更多内容

回答by Doh Simpson

node -rallows you to require a module when REPL starts up. NODE_PATHsets the module search path. So you can run something like this on your command line:

node -r允许您在 REPL 启动时需要一个模块。NODE_PATH设置模块搜索路径。所以你可以在命令行上运行这样的东西:

NODE_PATH=. node -r myscript.js

NODE_PATH=. node -r myscript.js

This should put you in a REPL with your script loaded.

这应该让你进入一个加载了脚本的 REPL。

回答by Daniel

I've recently started a project to create an advanced interactive shell for Node and associated languages like CoffeeScript. One of the features is loading a file or string in the context of the interpreter at startup which takes into account the loaded language.

我最近启动了一个项目,为 Node 和相关语言(如 CoffeeScript)创建一个高级交互式 shell。其中一项功能是在启动时在解释器的上下文中加载文件或字符串,这会考虑加载的语言。

http://danielgtaylor.github.com/nesh/

http://danielgtaylor.github.com/nesh/

Examples:

例子:

# Load a string (Javascript)
nesh -e 'var hello = function (name) { return "Hello, " + name; };'

# Load a string (CoffeeScript)
nesh -c -e 'hello = (name) -> "Hello, #{name}"'

# Load a file (Javascript)
nesh -e hello.js

# Load a file (CoffeeScript)
nesh -c -e hello.coffee

Then in the interpreter you can access the hellofunction.

然后在解释器中您可以访问该hello功能。

回答by fiddlemath

Edit: Ignore this. @jaywalking101's answer is much better. Do that instead.

编辑:忽略这一点。@jaywalking101 的回答要好得多。改为这样做。

If you're running from inside a Bash shell (Linux, OS X, Cygwin), then

如果您从 Bash shell(Linux、OS X、Cygwin)内部运行,那么

cat __preamble__.js - | node -i

will work. This also spews lots of noise from evaluating each line of preamble.js, but afterwords you land in an interactive shell in the context you want.

将工作。这也会在评估前导.js 的每一行时产生大量噪音,但是在您想要的上下文中进入交互式 shell 之后。

(The '-' to 'cat' just specifies "use standard input".)

(“-”到“cat”只是指定“使用标准输入”。)

回答by Felix Rabe

Similar answer to @slacktracer, but if you are fine using globalin your script, you can simply requireit instead of (learning and) using .load.

与@slacktracer 类似的答案,但如果您global在脚本中使用得很好,您可以简单地使用require它而不是(学习和)使用.load.

Example lib.js:

示例lib.js

global.x = 123;

Example nodesession:

示例node会话:

$ node
> require('./lib')
{}
> x
123

As a nice side-effect, you don't even have to do the var x = require('x'); 0dance, as module.exportsremains an empty object and thus the requireresult will not fill up your screen with the module's content.

作为一个很好的副作用,您甚至不必var x = require('x'); 0跳舞,因为它module.exports仍然是一个空对象,因此require结果不会用模块的内容填满您的屏幕。

回答by dthree

Vorpal.jswas built to do just this. It provides an API for building an interactive CLI in the context of your application.

Vorpal.js就是为了做到这一点而构建的。它提供了一个 API,用于在您的应用程序上下文中构建交互式 CLI。

It includes plugins, and one of these is Vorpal-REPL. This lets you type repland this will drop you into a REPL within the context of your application.

它包括插件,其中之一是Vorpal-REPL。这让您可以输入repl,这将使您进入应用程序上下文中的 REPL 。

Example to implement:

实现示例:

var vorpal = require('vorpal')();
var repl = require('vorpal-repl');
vorpal.use(repl).show();

// Now you do your custom code...

// If you want to automatically jump
// into REPl mode, just do this:
vorpal.exec('repl');

That's all!

就这样!

Disclaimer: I wrote Vorpal.

免责声明:我写了 Vorpal。

回答by neu-rah

nit-toollets you load a node module into the repl interactive and have access to inner module environment (join context) for development purposes

nit-tool允许您将节点模块加载到 repl 交互中,并可以访问内部模块环境(加入上下文)以进行开发

npm install nit-tool -g

回答by Evan

There isn't a way do this natively. You can either enter the node interactive shell nodeor run a script you have node myScrpt.js. @sarnold is right, in that if you want that for your app, you will need to make it yourself, and using the repl toolkit is helpful for that kind of thing

没有办法在本地做到这一点。您可以进入节点交互式 shellnode或运行您拥有的脚本node myScrpt.js。@sarnold 是对的,因为如果你想为你的应用程序使用它,你需要自己制作,使用 repl 工具包对这种事情很有帮助