Javascript 如何将我的脚本加载到 node.js REPL 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8425102/
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
How do I load my script into the node.js REPL?
提问by hugomg
I have a script foo.js
that contains some functions I want to play with in the REPL.
我有一个脚本foo.js
,其中包含我想在 REPL 中使用的一些函数。
Is there a way to have node execute my script and then jump into a REPL with all the declared globals, like I can with python -i foo.py
or ghci foo.hs
?
有没有办法让节点执行我的脚本,然后像我可以使用python -i foo.py
或那样使用所有声明的全局变量跳转到 REPL 中ghci foo.hs
?
回答by vossad01
There is still nothing built-in to provide the exact functionality you describe. However, an alternative to using require
it to use the .load
commandwithin the REPL, like such:
仍然没有任何内置功能可以提供您描述的确切功能。但是,使用require
它来使用REPL 中的.load
命令的替代方法,例如:
.load foo.js
It loads the file in line by line just as if you had typed it in the REPL. Unlike require
this pollutes the REPL history with the commands you loaded. However, it has the advantage of being repeatable because it is not cached like require
.
它逐行加载文件,就像您在 REPL 中键入它一样。与require
此不同,您加载的命令会污染 REPL 历史记录。但是,它具有可重复的优点,因为它不像require
.
Which is better for you will depend on your use case.
哪个更适合您将取决于您的用例。
Edit:It has limited applicability because it does not work in strict mode, but three years later I have learned that if your script does not have 'use strict'
, you can use eval
to load your scriptwithout polluting the REPL history:
编辑:它的适用性有限,因为它不能在严格模式下工作,但三年后我了解到,如果您的脚本没有'use strict'
,您可以使用eval
加载您的脚本而不会污染 REPL 历史记录:
var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())
回答by George Shalvashvili
i always use this command
我总是使用这个命令
node -i -e "$(< yourScript.js)"
works exactly as in Python without any packages.
与 Python 完全一样,没有任何包。
回答by dthree
I made Vorpal.js, which handles this problem by turning your node add into an interactive CLI. It supports a REPL extension, which drops you into a REPL within the context of your running app.
我制作了Vorpal.js,它通过将您的节点添加到交互式 CLI 来处理这个问题。它支持 REPL 扩展,可将您带入正在运行的应用程序上下文中的 REPL。
var vorpal = require('vorpal')();
var repl = require('vorpal-repl');
vorpal
.delimiter('myapp>')
.use(repl)
.show()
.parse(process.argv);
Then you can run the app and it will drop into a REPL.
然后你可以运行应用程序,它会进入一个 REPL。
$ node myapp.js repl
myapp> repl:
回答by Ashish Chaudhary
Another way is to define those functions as global.
另一种方法是将这些函数定义为全局函数。
global.helloWorld = function() { console.log("Hello World"); }
Then preload the file in the REPL as:
然后将 REPL 中的文件预加载为:
node -r ./file.js
Then the function helloWorld
can be accessed directly in the REPL.
然后helloWorld
可以直接在 REPL 中访问该函数。
回答by Thorsten Lorenz
I created replpadsince I got tired of reloading the script repeatedly.
我创建了replpad,因为我厌倦了反复重新加载脚本。
Simply install it via: npm install -g replpad
只需通过以下方式安装: npm install -g replpad
Then use it by running: replpad
然后通过运行使用它: replpad
If you want it to watch all files in the current and all subdirectories and pipe them into the repl when they change do: replpad .
如果您希望它查看当前和所有子目录中的所有文件,并在它们更改时将它们通过管道传输到 repl,请执行以下操作: replpad .
Check out the videos on the site to get a better idea of how it works and learn about some other nice features that it has like these:
查看网站上的视频,以更好地了解它的工作原理,并了解它具有的其他一些不错的功能,例如:
- access core module docs in the replvia the
dox()
function that is added to every core function, i.e.fs.readdir.dox()
- access user module readmes in the replvia the
dox()
function that is added to every module installed via npm, i.e.marked.dox()
- access function's highlighted source code, info on where function was defined(file, linenumber) and function
comments and/or jsdocswhere possible via the
src
property that is added to every function, i.e.express.logger.src
- scriptie-talkiesupport(see
.talk
command) - adds commands and keyboard shortcuts
- vim key bindings
- key map support
- parens matchingvia match token plugin
- appends code entered in repl back to filevia keyboard shortcut or
.append
command
- 通过
dox()
添加到每个核心功能的功能访问 repl 中的核心模块文档,即fs.readdir.dox()
- 通过
dox()
添加到通过 npm 安装的每个模块的函数访问 repl 中的用户模块自述文件,即marked.dox()
- 访问函数的突出显示源代码、函数定义位置的信息(文件、行号)和函数注释和/或jsdocs(尽可能通过
src
添加到每个函数的属性,即express.logger.src
- 脚本对讲机支持(见
.talk
命令) - 添加命令和键盘快捷键
- vim 键绑定
- 关键地图支持
- 通过匹配令牌插件匹配括号
- 通过键盘快捷键或
.append
命令将在 repl 中输入的代码附加回文件
回答by lfender6445
Why not load the file into an interactive node repl?
为什么不将文件加载到交互式节点 repl 中?
node -h
-e, --eval script evaluate script
-i, --interactive always enter the REPL even if stdin
node -e 'var client = require("./build/main/index.js"); console.log("Use `client` in repl")' -i
Then you can add to package.json scripts
然后你可以添加到 package.json 脚本
"repl": "node -e 'var client = require(\"./build/main/index.js\"); console.log(\"Use `client` in repl\")' -i",
tested using node v8.1.2
使用节点 v8.1.2 测试
回答by user1936097
replpad
is cool, but for a quick and easy way to load a file into node, import its variables and start a repl, you can add the following code to the end of your .js file
replpad
很酷,但是为了快速简便地将文件加载到节点,导入其变量并启动 repl,您可以将以下代码添加到 .js 文件的末尾
if (require.main === module){
(function() {
var _context = require('repl').start({prompt: '$> '}).context;
var scope = require('lexical-scope')(require('fs').readFileSync(__filename));
for (var name in scope.locals[''] )
_context[scope.locals[''][name]] = eval(scope.locals[''][name]);
for (name in scope.globals.exported)
_context[scope.globals.exported[name]] = eval(scope.globals.exported[name]);
})();
}
Now if your file is src.js
, running node src.js
will start node, load the file, start a REPL, and copy all the objects declared as var
at the top level as well as any exported globals.
The if (require.main === module)
ensures that this code will not be executed if src.js
is included through a require
statement. I fact, you can add any code you want to be excuted when you are running src.js
standalone for debugging purposes inside the if
statement.
现在,如果您的文件是src.js
,运行node src.js
将启动节点,加载文件,启动 REPL,并复制var
在顶层声明的所有对象以及任何导出的全局变量。的if (require.main === module)
,如果这个代码将不会被执行,确保src.js
通过包括require
声明。事实上,当您src.js
为了调试目的而在if
语句中独立运行时,您可以添加任何想要执行的代码。
回答by Ricardo Tomasi
Currently you can't do that directly, but you can mylib = require('./foo.js')
in the REPL. Remember methods are exported, not declared as globals.
目前你不能直接这样做,但你可以mylib = require('./foo.js')
在 REPL 中。记住方法是导出的,而不是声明为全局变量。
回答by Eliot
Here's a bash function version of George's answer:
这是乔治回答的 bash 函数版本 :
noderepl() {
FILE_CONTENTS="$(< )"
node -i -e "$FILE_CONTENTS"
}
If you put this in your ~/.bash_profile
you can use it like an alias, i.e.:
如果你把它放在你的~/.bash_profile
你可以像别名一样使用它,即:
noderepl foo.js
回答by corvid
Another suggestion that I do not see here: try this little bit of code
我在这里没有看到的另一个建议:试试这段代码
#!/usr/bin/env node
'use strict';
const repl = require('repl');
const cli = repl.start({ replMode: repl.REPL_MODE_STRICT });
cli.context.foo = require('./foo'); // injects it into the repl
Then you can simply run this script and it will include foo
as a variable
然后你可以简单地运行这个脚本,它会foo
作为一个变量包含