我爱编程

Node.js CLI

2017-08-18  本文已影响81人  喵不留行

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

$ node -v
v6.11.2
$ 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
  ......
$ node -e 'console.log("-e test")'
-e test
$ node -p 'console.log("-p test")'
-p test
undefinded
$ node -p '2+3'
5
$ 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
$ 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/******
>

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

$ node --inspect-brk
Debugger listening on ws://127.0.0.1:9229/*********
For help see https://nodejs.org/en/docs/inspector
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 可选项

常用的 Node.js CLI 环境变量

$ NODE_DEBUG=module,fs,http,timers node index.js

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

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

上一篇 下一篇

猜你喜欢

热点阅读