【问题标题】:Start up script for node.js replnode.js repl 的启动脚本
【发布时间】:2011-10-09 19:13:03
【问题描述】:

有没有办法配置 node.js 的 repl?我想在 repl 启动时自动要求 jquery 和下划线。是否有 node.js 在启动 repl 时加载的文件(noderc?)?

Python 中的等价物是编辑~/.ipython/ipy_user_conf.py

import_mod('sys os datetime re itertools functools')

【问题讨论】:

    标签: node.js startup read-eval-print-loop rc startupscript


    【解决方案1】:

    2017 年 2 月 - 虽然我同意接受的答案,但希望在此处添加更多评论。

    喜欢如下设置(从我的 Mac 上的主目录)

    .node ├── node_modules │   ├── lodash │   └── ramda ├── package.json └── repl.js

    那么 repl.js 可能如下所示:

    const repl = require('repl');
    
    let r = repl.start({
      ignoreUndefined: true,
      replMode: repl.REPL_MODE_STRICT
    });
    
    r.context.lodash = require('lodash');
    r.context.R = require('ramda');
    // add your dependencies here as you wish..
    

    最后,将别名放入您的 .bashrc.zshrc 文件等(取决于您的 shell 首选项) - 类似于:

    alias noder='node ~/.node/repl.js'

    现在,要使用此配置,您只需从命令行输入noder。上面,我还指定我总是喜欢在strict mode,不希望undefined打印到控制台进行声明等。

    有关repl 尤其是repl.start 选项的最新信息,请参阅here

    【讨论】:

    • 很好的答案,感谢您的详细说明!
    【解决方案2】:

    为了简单起见,这就是我拼凑起来的。

    repl.js:

    // things i want in repl
    global.reload = require('require-nocache')(module) // so I can reload modules as I edit them
    global.r = require('ramda') // <3 http://ramdajs.com/
    // launch, also capture ref to the repl, in case i want it later
    global.repl = require('repl').start()
    

    我可以用node repl 调用它,感觉不错,而且我不关心全局变量,因为我只是在 repl 中乱搞。

    【讨论】:

      【解决方案3】:

      可能是 Node.js 的一个新功能(因为这个问题已经有四年了),但 you can load and save repl history 喜欢 ipython。

      .break - While inputting a multi-line expression, sometimes you get lost or just don't care about completing it. .break will start over.
      .clear - Resets the context object to an empty object and clears any multi-line expression.
      .exit - Close the I/O stream, which will cause the REPL to exit.
      .help - Show this list of special commands.
      .save - Save the current REPL session to a file
          .save ./file/to/save.js
      .load - Load a file into the current REPL session.
          .load ./file/to/load.js
      

      我不知道如何在启动 shell 时自动执行此操作,但目前.load something 对我来说已经足够方便了。

      【讨论】:

        【解决方案4】:

        我今天试过了,但.start 需要一个参数。我也认为useGlobal:true 很重要。我最终使用:

        var myrepl=require('repl').start({useGlobal:true});
        myrepl.context['myObj']=require('./myObject');
        

        将此代码保存在test.js 中,我可以执行node test.js,然后在REPL 中访问myObj

        【讨论】:

        • 我需要这个来让@nicolaskruchten 的示例工作。谢谢你们!
        【解决方案5】:

        我不知道有任何这样的配置文件,但如果你想在 REPL 中使用模块 foobar,你可以创建一个文件 myrepl.js,其中包含:

        var myrepl = require("repl").start();
        ["foo", "bar"].forEach(function(modName){
            myrepl.context[modName] = require(modName); 
        });
        

        当你使用node myrepl.js 执行它时,你会得到一个包含可用模块的 REPL。

        有了这些知识,您可以将 #!/path/to/node 放在顶部并使其直接可执行,或者您可以修改您的 repl.js 模块版本(源代码可在 https://github.com/joyent/node/blob/master/lib/repl.js 获取以供检查)或其他任何内容 :)

        【讨论】:

        • 太棒了!我做了一个变体,用别名加载模块。 var modules = {jquery: '$', 下划线: '_und'}; for (var mod in modules) { my_repl.context[modules[mod]] = require(mod); }
        猜你喜欢
        • 1970-01-01
        • 2012-09-30
        • 1970-01-01
        • 2017-04-07
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 2017-01-19
        • 1970-01-01
        相关资源
        最近更新 更多