如何为节点设置预加载文件?

时间:2014-04-12 22:12:00

标签: node.js

有没有办法在每次运行node(交互式)之前预加载某个文件,就像.vimrc.bash_profile等一样?

我主要以交互方式使用node,并且我经常使用模块CSV,是否有办法避免每次开始require('./csv')时输入node

2 个答案:

答案 0 :(得分:5)

创建初始化文件(例如〜/ .noderc ):

var csv = require('csv');

// put a blank line in the end of the file

现在将此行添加到您的shell配置( .bashrc / .zshrc /您使用的任何shell):

alias nodei="cat ~/.noderc - | node -i"

中提琴!

答案 1 :(得分:4)

@Ilan Frumer提供了一种方法。我想我会在这里给出另一个选择:建立自己的REPL

来自their documentation。您可以找到一种编写自己的repl的方法。您可以在其交互之前和之后添加任何脚本,甚至可以使用一些高级API。

例如,我在.noderc.js下创建了一个名为~的文件,如下所示

repl = require('repl');

myFunc = function(){
    console.log("Hello, there!");
};

repl.start("> ");

你可以继续alias nodei="node ~/.noderc.js"

$ nodei
> myFunc()
Hello, there!
undefined