Node.js CLI

man node 命令將列出 Node.js CLI 的有關(guān)信息,包括所有允許的 CLI 可選項(xiàng)(options)和 環(huán)境變量(Environment Variables)。

NAME
       node - Server-side JavaScript runtime



SYNOPSIS
       node [options] [v8 options] [script.js | -e "script"] [arguments]
       node debug [script.js | -e "script" | <host>:<port>] ...
       node [--v8-options]

       Execute without arguments to start the REPL.



DESCRIPTION
       Node.js  is  a  set  of  libraries  for JavaScript which allows it to be used outside of the
       browser. It is primarily focused on creating simple,  easy  to  build  network  clients  and
       servers.



OPTIONS
       -v, --version
              Print node's version.


       -h, --help
              Print  node  command  line  options.  The output of this option is less detailed than
              this document.


       -e, --eval "script"
              Evaluate the following argument as JavaScript.


       -p, --print "script"
              Identical to -e but prints the result.


       -c, --check
    ......

常用的 Node.js CLI 可選項(xiàng)

  • -v 或 --version:查看當(dāng)前使用的 node 版本
$ node -v
v6.11.2
  • -h 或 --help:查看幫助,快捷查看部分支持的可選項(xiàng)和環(huán)境變量
$ node -h
Usage: node [options] [ -e script | script.js ] [arguments]
       node debug script.js [arguments]

Options:
  -v, --version         print Node.js version
  -e, --eval script     evaluate script
  -p, --print           evaluate script and print result
  -c, --check           syntax check script without executing
  -i, --interactive     always enter the REPL even if stdin
                        does not appear to be a terminal
  ......
  • -e 或 --eval '代碼'(>=v0.5.2):命令行執(zhí)行代碼规丽。
$ node -e 'console.log("-e test")'
-e test
  • -p 或 --print '代碼'(>=v0.6.4):輸出結(jié)果的帶命令行執(zhí)行代碼
$ node -p 'console.log("-p test")'
-p test
undefinded
$ node -p '2+3'
5
  • -c 或 --check(>=v5.0.0):檢查指定腳本(文件)的語法侵蒙,不實(shí)際執(zhí)行。
$ node -c 'index.js'
/Users/cxswow/Documents/work/tmp/test/index.js:3
console.log('a'
            ^^^
SyntaxError: missing ) after argument list
    at startup (bootstrap_node.js:134:11)
    at bootstrap_node.js:535:3
  • --inspect[=host:port] (>=v6.3.0):對指定地址和端口激活
    inspector(實(shí)時調(diào)試觀察) 块攒,默認(rèn)地址端口:127.0.0.1:9229。調(diào)試工具與 Node.js 實(shí)例通過一個使用 Chrome Debugging Protocol 的 tcp 端口進(jìn)行通信佃乘。
$ node --inspect
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/******
>

在瀏覽器里打開那個鏈接將看到和在谷歌瀏覽器里打開檢查以后類似的調(diào)試器囱井。

  • --inspect-brk[=host:port](>=v7.6.0):與 --inspect 類似,不同的是會在用戶代碼的第一行暫停趣避。
$ node --inspect-brk
Debugger listening on ws://127.0.0.1:9229/*********
For help see https://nodejs.org/en/docs/inspector
  • --zero-fill-buffers(>=v6.0.0):在新建的 Buffer 實(shí)例時全都用 0 填充庞呕,因?yàn)樾陆ǖ?Buffer 實(shí)例里可能包含敏感數(shù)據(jù)。

  • --prof-process(>=v6.0.0):輸出 V8 profiler 的輸出程帕。
    node --prof-process index.js 命令正常運(yùn)行 index.js 然后生成一個 isolate- 開頭的 log 文件住练,然后就可以運(yùn)行 node --prof-process isolate-0x103000000-v8.log(剛生成的文件名) > output.txt 來生成 output.txt 文件,文件里包含 V8 profiler 的各種信息愁拭,如 JavaScript 耗時讲逛、C++ 層耗時等。

Statistical profiling result from isolate-0x103000000-v8.log, (312 ticks, 14 unaccounted, 0 excluded).

 [Shared libraries]:
   ticks  total  nonlib   name
     43   13.8%          /usr/lib/system/libsystem_malloc.dylib
      5    1.6%          /usr/lib/system/libsystem_kernel.dylib
      3    1.0%          /usr/lib/system/libsystem_c.dylib
      2    0.6%          /usr/lib/system/libsystem_platform.dylib
      1    0.3%          /usr/lib/system/libsystem_pthread.dylib
      1    0.3%          /usr/lib/libc++abi.dylib
      1    0.3%          /usr/lib/libc++.1.dylib

 [JavaScript]:
   ticks  total  nonlib   name
      1    0.3%    0.4%  Builtin: StoreICStrict_Uninitialized

 [C++]:
   ticks  total  nonlib   name
     21    6.7%    8.2%  t node::(anonymous namespace)::ContextifyScript::New(v8::FunctionCallbackInfo<v8::Value> const&)
......

V8 可選項(xiàng)

node --v8-options 命令將輸出所有可用的 V8 可選項(xiàng)(超級多A氩骸U祷臁)。

SSE3=1 SSSE3=1 SSE4_1=1 SAHF=1 AVX=0 FMA3=0 BMI1=0 BMI2=0 LZCNT=0 POPCNT=1 ATOM=0
Usage:
  shell [options] -e string
    execute string in V8
  shell [options] file1 file2 ... filek
    run JavaScript scripts in file1, file2, ..., filek
  shell [options]
  shell [options] --shell [file1 file2 ... filek]
    run an interactive JavaScript shell
  d8 [options] file1 file2 ... filek
  d8 [options]
  d8 [options] --shell [file1 file2 ... filek]
    run the new debugging shell

Options:
  --experimental_extras (enable code compiled in via v8_experimental_extra_library_files)
        type: bool  default: false
  --use_strict (enforce strict mode)
        type: bool  default: false
  --es_staging (enable test-worthy harmony features (for internal use only))
        type: bool  default: false
  --harmony (enable all completed harmony features)
        type: bool  default: false
  --harmony_shipping (enable all shipped harmony features)
        type: bool  default: true
  --harmony_array_prototype_values (enable "harmony Array.prototype.values" (in progress))
        type: bool  default: false
  --harmony_function_sent (enable "harmony function.sent" (in progress))
        type: bool  default: false
  --harmony_tailcalls (enable "harmony tail calls" (in progress))
        type: bool  default: false
  --harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress))
        type: bool  default: false
  --harmony_do_expressions (enable "harmony do-expressions" (in progress))
        type: bool  default: false
  --harmony_class_fields (enable "harmony public fields in class literals" (in progress))
        type: bool  default: false
  --harmony_async_iteration (enable "harmony async iteration" (in progress))
        type: bool  default: false
  --harmony_dynamic_import (enable "harmony dynamic import" (in progress))
        type: bool  default: false
  --harmony_promise_finally (enable "harmony Promise.prototype.finally" (in progress))
        type: bool  default: false
......

常用 V8 可選項(xiàng)

  • --harmony:JavaScript 下一個版本是 Harmony惜论,這個可選項(xiàng)讓我們可以使用下一個版本已經(jīng)實(shí)現(xiàn)的功能许赃,像是 Async 之類的。
  • --max_old_space_size:可以設(shè)置舊空間堆最大的大小来涨,直接影響進(jìn)程可以分配的內(nèi)存图焰。在低內(nèi)存環(huán)境下很有用的一個選項(xiàng)。
  • --optimize_for_size:和之前一個可選項(xiàng)使用情景類似蹦掐,這個選項(xiàng)可以指示 V8 犧牲一些性能來優(yōu)化內(nèi)存空間大小技羔。

常用的 Node.js CLI 環(huán)境變量

  • NODE_DEBUG=module[,…]:列出的模塊將輸出 debug 信息,模塊名字之間用逗號分隔卧抗。
$ NODE_DEBUG=module,fs,http,timers node index.js

不是所有的原生模塊都支持 debug 輸出藤滥,目前支持的模塊有:
cluster, net, http, fs, tls, module, timers。

  • NODE_PATH=path[:...]:添加額外的 Node.js 尋找模塊的地址社裆,不同地址之間用冒號分隔( windows 上面用分號分隔)拙绊。

參考:
Node.js v6.11.2 Documentation
Mastering the Node.js CLI & Command Line Options

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子标沪,更是在濱河造成了極大的恐慌榄攀,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件金句,死亡現(xiàn)場離奇詭異檩赢,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)违寞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門贞瞒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人趁曼,你說我怎么就攤上這事军浆。” “怎么了挡闰?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵乒融,是天一觀的道長。 經(jīng)常有香客問我尿这,道長簇抵,這世上最難降的妖魔是什么庆杜? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任射众,我火速辦了婚禮,結(jié)果婚禮上晃财,老公的妹妹穿的比我還像新娘叨橱。我一直安慰自己,他們只是感情好断盛,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布罗洗。 她就那樣靜靜地躺著,像睡著了一般钢猛。 火紅的嫁衣襯著肌膚如雪伙菜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天命迈,我揣著相機(jī)與錄音贩绕,去河邊找鬼。 笑死壶愤,一個胖子當(dāng)著我的面吹牛淑倾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播征椒,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼娇哆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起碍讨,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤治力,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后勃黍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體琴许,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年溉躲,在試婚紗的時候發(fā)現(xiàn)自己被綠了榜田。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡锻梳,死狀恐怖箭券,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情疑枯,我是刑警寧澤辩块,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站荆永,受9級特大地震影響废亭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜具钥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一豆村、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧骂删,春花似錦掌动、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至欧瘪,卻和暖如春眷射,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背佛掖。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工妖碉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人苦囱。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓嗅绸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親撕彤。 傳聞我的和親對象是個殘疾皇子鱼鸠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容