Node.js CLI

man node 命令将列出 Node.js CLI 的有关信息,包括所有允许的 CLI 可选项(options)和 环境变量(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 可选项

  • -v 或 --version:查看当前使用的 node 版本
$ node -v
v6.11.2
  • -h 或 --help:查看帮助,快捷查看部分支持的可选项和环境变量
$ 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):命令行执行代码。
$ node -e 'console.log("-e test")'
-e test
  • -p 或 --print '代码'(>=v0.6.4):输出结果的带命令行执行代码
$ node -p 'console.log("-p test")'
-p test
undefinded
$ node -p '2+3'
5
  • -c 或 --check(>=v5.0.0):检查指定脚本(文件)的语法,不实际执行。
$ 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(实时调试观察) ,默认地址端口:127.0.0.1:9229。调试工具与 Node.js 实例通过一个使用 Chrome Debugging Protocol 的 tcp 端口进行通信。
$ 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/******
>

在浏览器里打开那个链接将看到和在谷歌浏览器里打开检查以后类似的调试器。

  • --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 实例时全都用 0 填充,因为新建的 Buffer 实例里可能包含敏感数据。

  • --prof-process(>=v6.0.0):输出 V8 profiler 的输出。
    node --prof-process index.js 命令正常运行 index.js 然后生成一个 isolate- 开头的 log 文件,然后就可以运行 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 可选项

node --v8-options 命令将输出所有可用的 V8 可选项(超级多!!)。

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 可选项

  • --harmony:JavaScript 下一个版本是 Harmony,这个可选项让我们可以使用下一个版本已经实现的功能,像是 Async 之类的。
  • --max_old_space_size:可以设置旧空间堆最大的大小,直接影响进程可以分配的内存。在低内存环境下很有用的一个选项。
  • --optimize_for_size:和之前一个可选项使用情景类似,这个选项可以指示 V8 牺牲一些性能来优化内存空间大小。

常用的 Node.js CLI 环境变量

  • 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容