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):對(duì)指定地址和端口激活
    inspector(實(shí)時(shí)調(diào)試觀察) ,默認(rèn)地址端口:127.0.0.1:9229。調(diào)試工具與 Node.js 實(shí)例通過一個(gè)使用 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/******
>

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

  • --inspect-brk[=host:port](>=v7.6.0):與 --inspect 類似,不同的是會(huì)在用戶代碼的第一行暫停。
$ 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í)例時(shí)全都用 0 填充,因?yàn)樾陆ǖ?Buffer 實(shí)例里可能包含敏感數(shù)據(jù)。

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

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)(超級(jí)多?。。?。

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

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

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

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

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容