2011-07-20 18 views
17

node.jsのreplを設定する方法はありますか?私はjqueryとアンダースコアが必要なのは、replが始まるたびに自動的に必要です。レプリケーションを開始するときにnode.jsがロードするファイル(noderc?)がありますか?私はそのような設定ファイルを知りませんが、あなたはモジュールfoobarはREPLで利用できるようにしたい場合は、次のことができnode.jsのスクリプトを起動repl

import_mod('sys os datetime re itertools functools') 

答えて

16

:Pythonで

同等にして~/.ipython/ipy_user_conf.pyを編集することですあなたはnode myrepl.jsとそれを実行すると、使用可能なこれらのモジュールとのREPLを得る

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

を、あなた:含むファイルmyrepl.jsを作成します。

この知識で武装して、あなたが一番上に#!/path/to/nodeを入れて、実行ファイルを直接それを作る、またはあなたが(検査のためhttps://github.com/joyent/node/blob/master/lib/repl.jsで利用可能なソース)repl.jsモジュールのバージョンを変更することができたり、何でも:)

+0

優秀!私は別名でモジュールを読み込むバリエーションを作った。 var modules = {jquery: '$'、アンダースコア: '_und'}; (モジュール内のvar mod){ my_repl.context [modules [mod]] = require(mod); } – hekevintran

4

I今日は試しましたが、.startには引数が必要です。また、私はuseGlobal:trueが重要だったと思います。私が使用して巻き上げる:

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

保存このコードはtest.jsに、私はその後、REPLでアクセスmyObjnode test.jsを行うことができます。

+0

@ nicolaskruchtenの例がうまく動作するには、これが必要でした。両方のおかげで! – Nate

2

(この質問は4歳ですが)Node.jsの新しい機能かもしれませんが、ipythonのようなyou can load and save repl historyです。

.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 

私はシェルを起動するときに自動的にこれを実行する方法を見つけ出すことはできませんが、.load somethingは、現時点では十分な私にとっては便利です。

1

ここに簡単に書いておきます。

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で周りのmessinていますので、私は、グローバル気にしないでください。

2

2月2017 - 私は受け入れられた答えに同意しますが、もう少し解説を追加したいと思います。その後、セットアップしたいと

(私の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ファイルにエイリアスを置く - 何かのように:

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

へこの設定を使用するには、コマンドラインから「noder」と入力するだけです。上記の、私はまた、私はいつもstrict modeになりたい、とreplに関する最新情報については宣言のコンソールに出力undefinedなど

を望んでいないことを指定し、特にrepl.startオプションはhere

を見ます
関連する問題