如何将我的脚本加载到node.js REPL中? [英] How do I load my script into the node.js REPL?

查看:131
本文介绍了如何将我的脚本加载到node.js REPL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本 foo.js ,其中包含我想在REPL中使用的一些函数。

I have a script foo.js that contains some functions I want to play with in the REPL.

有没有办法让节点执行我的脚本,然后跳转到包含所有声明的全局变量的REPL,就像我可以使用 python -i foo.py ghci foo.hs

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?

推荐答案

仍然没有内置的提供您描述的确切功能。但是,使用的替代方法需要它使用 .load 命令,如下所示:

There is still nothing built-in to provide the exact functionality you describe. However, an alternative to using require it to use the .load command within the REPL, like such:

.load foo.js

它逐行加载文件,就像你输入的一样它在REPL中。与 require 不同,这会使用您加载的命令污染REPL历史记录。但是,它具有可重复性的优点,因为它不像 require 那样缓存。

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.

哪个更适合你将取决于您的使用案例。

Which is better for you will depend on your use case.

编辑它的适用性有限,因为它没有在严格模式下工作,但三年后,我了解到如果你的脚本没有'use strict',你可以使用 eval 加载脚本而不会污染REPL历史记录:

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 script without polluting the REPL history:

var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())

这篇关于如何将我的脚本加载到node.js REPL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆