我有一个脚本foo.js
,里面包含一些函数想要在REPL中使用。
有没有一种方法可以让Node执行我的脚本,然后跳入REPL并具有所有已声明的全局变量,就像我可以使用python -i foo.py
或ghci foo.hs
一样?
我有一个脚本foo.js
,里面包含一些函数想要在REPL中使用。
有没有一种方法可以让Node执行我的脚本,然后跳入REPL并具有所有已声明的全局变量,就像我可以使用python -i foo.py
或ghci foo.hs
一样?
目前没有内置功能可以提供您描述的精确功能。不过,使用REPL中的.load
命令是使用require
的替代方法,用法如下:
.load foo.js
require
不同,它会将加载的命令弄脏REPL历史记录。但是,它具有可重复性的优点,因为它不像require
一样被缓存。编辑:它的适用范围有限,因为它不适用于严格模式,但是三年后我学到了,如果你的脚本没有使用'use strict'
,你可以使用eval
来加载你的脚本,而不会污染REPL历史记录:
var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())
我总是使用这个命令
node -i -e "$(< yourScript.js)"
与Python中没有任何包时的功能完全相同。
我制作了Vorpal.js,它通过将您的 node 应用程序转换为交互式命令行界面来解决这个问题。它支持 REPL 扩展,该扩展可以让您在运行应用程序的上下文中进入 REPL。
var vorpal = require('vorpal')();
var repl = require('vorpal-repl');
vorpal
.delimiter('myapp>')
.use(repl)
.show()
.parse(process.argv);
然后您可以运行该应用程序,它将进入REPL。
$ node myapp.js repl
myapp> repl:
另一种方法是将这些函数定义为全局函数。
global.helloWorld = function() { console.log("Hello World"); }
然后在REPL中预加载该文件:
node -r ./file.js
那么函数helloWorld
可以直接在REPL中访问。
noderepl() {
FILE_CONTENTS="$(< $1 )"
node -i -e "$FILE_CONTENTS"
}
~/.bash_profile
中,您可以像使用别名一样使用它,例如:noderepl foo.js
npm install -g replpad
然后通过运行replpad
来使用它。replpad .
请查看网站上的视频以更好地了解其工作原理,并了解一些其他不错的功能,比如这些:
dox()
函数,可以在repl中访问核心模块文档,例如fs.readdir.dox()
dox()
函数,在repl中访问用户模块readme,例如marked.dox()
src
属性,访问函数的高亮源代码、函数定义位置(文件、行号)和函数注释或jsdocs(如果可能),例如express.logger.src
.talk
命令).append
命令将在repl中输入的代码附加回文件CXX=clang++ npm install replpad
来解决错误 g++: error: unrecognized command line option '-stdlib=libc++'
。 - ShadSterlingnode -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
然后您可以在package.json中添加脚本
"repl": "node -e 'var client = require(\"./build/main/index.js\"); console.log(\"Use `client` in repl\")' -i",
本文使用 node v8.1.2 进行测试。
node -i -r "./build/main/index.js"
呢? - Bernardo Dal Corno目前直接无法实现,但你可以在REPL中使用 mylib = require('./foo.js')
。请记住,方法是导出的,而不是声明为全局变量。
.load my_work.js
更好,尽管需要一些额外的exports.working_var = ...
声明,因为REPL在某些完全有效的javascript代码上会出现错误,例如多行注释(至少在我的readline
配置中是如此)。 - chbrownif (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]);
})();
}
如果您的文件名为src.js
,运行node src.js
将启动node,加载文件,启动REPL,并复制声明为var
的所有对象以及任何导出的全局变量。
if (require.main === module)
确保此代码不会在通过require
语句包含src.js
时执行。实际上,您可以在if
语句内添加任何要在单独调试src.js
时执行的代码。
这里还有一个建议:尝试使用以下代码:
#!/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
然后你只需要运行这个脚本,它将会把foo
作为一个变量包含进来。
__dirname
才能让它工作。此外,我将代码编写为一个 shell 脚本(为了可重用性)。否则,这是个很好的解决方案! - Dave F