Command-line API
目录
- Synopsis
- Program entry point
- Options
- -
- --
- --abort-on-uncaught-exception
- --build-snapshot
- --completion-bash
- -C=condition, --conditions=condition
- --cpu-prof
- --cpu-prof-dir
- --cpu-prof-interval
- --cpu-prof-name
- --diagnostic-dir=directory
- --disable-proto=mode
- --disallow-code-generation-from-strings
- --dns-result-order=order
- --enable-fips
- --enable-source-maps
- --experimental-import-meta-resolve
- --experimental-loader=module
- --experimental-network-imports
- --experimental-policy
- --no-experimental-fetch
- --no-experimental-global-webcrypto
- --no-experimental-global-customevent
- --no-experimental-repl-await
- --experimental-shadow-realm
- --experimental-vm-modules
- --experimental-wasi-unstable-preview1
- --experimental-wasm-modules
- --force-context-aware
- --force-fips
- --frozen-intrinsics
- --force-node-api-uncaught-exceptions-policy
- --heapsnapshot-near-heap-limit=max_count
- --heapsnapshot-signal=signal
- --heap-prof
- --heap-prof-dir
- --heap-prof-interval
- --heap-prof-name
- --icu-data-dir=file
- --import=module
- --input-type=type
- --inspect-brk[=[host:]port]
- --inspect-port=[host:]port
- --inspect[=[host:]port]
- --inspect-publish-uid=stderr,http
- --insecure-http-parser
- --jitless
- --max-http-header-size=size
- --napi-modules
- --no-addons
- --no-deprecation
- --no-extra-info-on-fatal-exception
- --no-force-async-hooks-checks
- --no-global-search-paths
- --no-warnings
- --node-memory-debug
- --openssl-config=file
- --openssl-shared-config
- --openssl-legacy-provider
- --pending-deprecation
- --policy-integrity=sri
- --preserve-symlinks
- --preserve-symlinks-main
- --prof
- --prof-process
- --redirect-warnings=file
- --report-compact
- --report-dir=directory, report-directory=directory
- --report-filename=filename
- --report-on-fatalerror
- --report-on-signal
- --report-signal=signal
- --report-uncaught-exception
- --secure-heap=n
- --secure-heap-min=n
- --snapshot-blob=path
- --test
- --test-name-pattern
- --test-only
- --throw-deprecation
- --title=title
- --tls-cipher-list=list
- --tls-keylog=file
- --tls-max-v1.2
- --tls-max-v1.3
- --tls-min-v1.0
- --tls-min-v1.1
- --tls-min-v1.2
- --tls-min-v1.3
- --trace-atomics-wait
- --trace-deprecation
- --trace-event-categories
- --trace-event-file-pattern
- --trace-events-enabled
- --trace-exit
- --trace-sigint
- --trace-sync-io
- --trace-tls
- --trace-uncaught
- --trace-warnings
- --track-heap-objects
- --unhandled-rejections=mode
- --update-assert-snapshot
- --use-bundled-ca, --use-openssl-ca
- --use-largepages=mode
- --v8-options
- --v8-pool-size=num
- --watch
- --watch-path
- --zero-fill-buffers
- -c, --check
- -e, --eval "script"
- -h, --help
- -i, --interactive
- -p, --print "script"
- -r, --require module
- -v, --version
- Environment variables
- FORCE_COLOR=[1, 2, 3]
- NODE_DEBUG=module[,…]
- NODE_DEBUG_NATIVE=module[,…]
- NODE_DISABLE_COLORS=1
- NODE_EXTRA_CA_CERTS=file
- NODE_ICU_DATA=file
- NODE_NO_WARNINGS=1
- NODE_OPTIONS=options...
- NODE_PATH=path[:…]
- NODE_PENDING_DEPRECATION=1
- NODE_PENDING_PIPE_INSTANCES=instances
- NODE_PRESERVE_SYMLINKS=1
- NODE_REDIRECT_WARNINGS=file
- NODE_REPL_HISTORY=file
- NODE_REPL_EXTERNAL_MODULE=file
- NODE_SKIP_PLATFORM_CHECK=value
- NODE_TLS_REJECT_UNAUTHORIZED=value
- NODE_V8_COVERAGE=dir
- NO_COLOR=<any>
- OPENSSL_CONF=file
- SSL_CERT_DIR=dir
- SSL_CERT_FILE=file
- TZ
- UV_THREADPOOL_SIZE=size
- Useful V8 options
自 v5.9.1 版本开始新增
Node.js comes with a variety of CLI options. These options expose built-in debugging, multiple ways to execute scripts, and other helpful runtime options.
To view this documentation as a manual page in a terminal, run man node
.
Synopsis
node [options] [V8 options] [<program-entry-point> | -e "script" | -] [--] [arguments]
node inspect [<program-entry-point> | -e "script" | <host>:<port>] …
node --v8-options
Execute without arguments to start the REPL.
For more info about node inspect
, see the debugger documentation.
Program entry point
The program entry point is a specifier-like string. If the string is not an absolute path, it's resolved as a relative path from the current working directory. That path is then resolved by CommonJS module loader. If no corresponding file is found, an error is thrown.
If a file is found, its path will be passed to the ECMAScript module loader under any of the following conditions:
- The program was started with a command-line flag that forces the entry point to be loaded with ECMAScript module loader.
- The file has an
.mjs
extension. - The file does not have a
.cjs
extension, and the nearest parentpackage.json
file contains a top-level"type"
field with a value of"module"
.
Otherwise, the file is loaded using the CommonJS module loader. See Modules loaders for more details.
ECMAScript modules loader entry point caveat
When loading ECMAScript module loader loads the program entry point, the node
command will only accept as input only files with .js
, .mjs
, or .cjs
extensions; and with .wasm
extensions when
--experimental-wasm-modules
is enabled.
Options
All options, including V8 options, allow words to be separated by both
dashes (-
) or underscores (_
). For example, --pending-deprecation
is
equivalent to --pending_deprecation
.
If an option that takes a single value (such as --max-http-header-size
) is
passed more than once, then the last passed value is used. Options from the
command line take precedence over options passed through the NODE_OPTIONS
environment variable.
M -
自 v8.0.0 版本开始新增
Alias for stdin. Analogous to the use of -
in other command-line utilities,
meaning that the script is read from stdin, and the rest of the options
are passed to that script.
M --
自 v6.11.0 版本开始新增
Indicate the end of node options. Pass the rest of the arguments to the script. If no script filename or eval/print script is supplied prior to this, then the next argument is used as a script filename.
M --abort-on-uncaught-exception
自 v0.10.8 版本开始新增
Aborting instead of exiting causes a core file to be generated for post-mortem
analysis using a debugger (such as lldb
, gdb
, and mdb
).
If this flag is passed, the behavior can still be set to not abort through
process.setUncaughtExceptionCaptureCallback()
(and through usage of the
node:domain
module that uses it).
M --build-snapshot
自 v18.8.0 版本开始新增
Generates a snapshot blob when the process exits and writes it to
disk, which can be loaded later with --snapshot-blob
.
When building the snapshot, if --snapshot-blob
is not specified,
the generated blob will be written, by default, to snapshot.blob
in the current working directory. Otherwise it will be written to
the path specified by --snapshot-blob
.
BASH
The v8.startupSnapshot
API can be used to specify an entry point at
snapshot building time, thus avoiding the need of an additional entry
script at deserialization time:
BASH
For more information, check out the v8.startupSnapshot
API documentation.
Currently the support for run-time snapshot is experimental in that:
- User-land modules are not yet supported in the snapshot, so only one single file can be snapshotted. Users can bundle their applications into a single script with their bundler of choice before building a snapshot, however.
- Only a subset of the built-in modules work in the snapshot, though the Node.js core test suite checks that a few fairly complex applications can be snapshotted. Support for more modules are being added. If any crashes or buggy behaviors occur when building a snapshot, please file a report in the Node.js issue tracker and link to it in the tracking issue for user-land snapshots.
M --completion-bash
自 v10.12.0 版本开始新增
Print source-able bash completion script for Node.js.
BASH
M -C=condition
, --conditions=condition
自 v14.9.0, v12.19.0 版本开始新增
Enable experimental support for custom conditional exports resolution conditions.
Any number of custom string condition names are permitted.
The default Node.js conditions of "node"
, "default"
, "import"
, and
"require"
will always apply as defined.
For example, to run a module with "development" resolutions:
BASH
M --cpu-prof
自 v12.0.0 版本开始新增
Starts the V8 CPU profiler on start up, and writes the CPU profile to disk before exit.
If --cpu-prof-dir
is not specified, the generated profile is placed
in the current working directory.
If --cpu-prof-name
is not specified, the generated profile is
named CPU.$yyyymmdd.$hhmmss.$pid.$tid.$seq.cpuprofile
.
BASH
M --cpu-prof-dir
自 v12.0.0 版本开始新增
Specify the directory where the CPU profiles generated by --cpu-prof
will
be placed.
The default value is controlled by the
--diagnostic-dir
command-line option.
M --cpu-prof-interval
自 v12.2.0 版本开始新增
Specify the sampling interval in microseconds for the CPU profiles generated
by --cpu-prof
. The default is 1000 microseconds.
M --cpu-prof-name
自 v12.0.0 版本开始新增
Specify the file name of the CPU profile generated by --cpu-prof
.
M --diagnostic-dir=directory
Set the directory to which all diagnostic output files are written. Defaults to current working directory.
Affects the default output directory of:
M --disable-proto=mode
自 v13.12.0, v12.17.0 版本开始新增
Disable the Object.prototype.__proto__
property. If mode
is delete
, the
property is removed entirely. If mode
is throw
, accesses to the
property throw an exception with the code ERR_PROTO_ACCESS
.
M --disallow-code-generation-from-strings
自 v9.8.0 版本开始新增
Make built-in language features like eval
and new Function
that generate
code from strings throw an exception instead. This does not affect the Node.js
node:vm
module.
M --dns-result-order=order
历史
版本 | 历史变更 |
---|---|
v17.0.0 | Changed default value to `verbatim`. |
v16.4.0, v14.18.0 | 自 v16.4.0, v14.18.0 版本开始新增 |
Set the default value of verbatim
in dns.lookup()
and
dnsPromises.lookup()
. The value could be:
ipv4first
: sets defaultverbatim
false
.verbatim
: sets defaultverbatim
true
.
The default is verbatim
and dns.setDefaultResultOrder()
have higher
priority than --dns-result-order
.
M --enable-fips
自 v6.0.0 版本开始新增
Enable FIPS-compliant crypto at startup. (Requires Node.js to be built against FIPS-compatible OpenSSL.)