Start up script for node.js repl

前端 未结 5 670
醉酒成梦
醉酒成梦 2020-12-30 02:01

Is there a way configure node.js\'s repl? I want to require jquery and underscore automatically whenever the repl starts. Is there a file (noderc?) that node.js loads when i

5条回答
  •  没有蜡笔的小新
    2020-12-30 02:56

    Feb 2017 - whilst I agree with the accepted answer, wished to add a little more commentary here.

    Like to setup as follows (from home directory on my Mac)

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

    Then repl.js may look like as follows:

    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..
    

    And finally, put an alias into your .bashrc or .zshrc file etc (depending on your shell prefs) - something like:

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

    Now, to use this configuration, you just have to type noder from the command line. Above, I also specified that I'd always like to be in strict mode, and do not want undefined printed to the console for declarations etc.

    For up-to-date information on repl and in particular repl.start options see here

提交回复
热议问题