首页 > 代码库 > Node.js-Usage & Example

Node.js-Usage & Example

Usage#

node [options] [v8 options] [script.js | -e "script"] [arguments]

Please see the Command Line Options document for information about different options and ways to run scripts with Node.js.

Example#

An example of a web server written with Node.js which responds with ‘Hello World‘:

const http = require(‘http‘);const hostname = ‘127.0.0.1‘;const port = 3000;const server = http.createServer((req, res) => {  res.statusCode = 200;  res.setHeader(‘Content-Type‘, ‘text/plain‘);  res.end(‘Hello World\n);});server.listen(port, hostname, () => {  console.log(`Server running at http://${hostname}:${port}/`);});

To run the server, put the code into a file called example.js and execute it with Node.js:

$ node example.jsServer running at http://127.0.0.1:3000/

All of the examples in the documentation can be run similarly.

  • Command Line Options
    • Synopsis
    • Options
      • -v--version
      • -h--help
      • -e--eval "script"
      • -p--print "script"
      • -c--check
      • -i--interactive
      • -r--require module
      • --no-deprecation
      • --trace-deprecation
      • --throw-deprecation
      • --no-warnings
      • --trace-warnings
      • --trace-sync-io
      • --zero-fill-buffers
      • --preserve-symlinks
      • --track-heap-objects
      • --prof-process
      • --v8-options
      • --tls-cipher-list=list
      • --enable-fips
      • --force-fips
      • --openssl-config=file
      • --icu-data-dir=file
    • Environment Variables
      • NODE_DEBUG=module[,…]
      • NODE_PATH=path[:…]
      • NODE_DISABLE_COLORS=1
      • NODE_ICU_DATA=http://www.mamicode.com/file
      • NODE_REPL_HISTORY=file
      • NODE_TTY_UNSAFE_ASYNC=1
      • NODE_EXTRA_CA_CERTS=file

Command Line Options#

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 your terminal, run man node.

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.

For more info about node debug, please see the debugger documentation.

Options#

-v--version#

Added in: v0.1.3

Print node‘s version.

-h--help#

Added in: v0.1.3

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

-e--eval "script"#

Added in: v0.5.2

Evaluate the following argument as JavaScript. The modules which are predefined in the REPL can also be used in script.

-p--print "script"#

Added in: v0.6.4

Identical to -e but prints the result.

-c--check#

Added in: v5.0.0

Syntax check the script without executing.

-i--interactive#

Added in: v0.7.7

Opens the REPL even if stdin does not appear to be a terminal.

-r--require module#

Added in: v1.6.0

Preload the specified module at startup.

Follows require()‘s module resolution rules. module may be either a path to a file, or a node module name.

--no-deprecation#

Added in: v0.8.0

Silence deprecation warnings.

--trace-deprecation#

Added in: v0.8.0

Print stack traces for deprecations.

--throw-deprecation#

Added in: v0.11.14

Throw errors for deprecations.

--no-warnings#

Added in: v6.0.0

Silence all process warnings (including deprecations).

--trace-warnings#

Added in: v6.0.0

Print stack traces for process warnings (including deprecations).

--trace-sync-io#

Added in: v2.1.0

Prints a stack trace whenever synchronous I/O is detected after the first turn of the event loop.

--zero-fill-buffers#

Added in: v6.0.0

Automatically zero-fills all newly allocated Buffer and SlowBuffer instances.

--preserve-symlinks#

Added in: v6.3.0

Instructs the module loader to preserve symbolic links when resolving and caching modules.

By default, when Node.js loads a module from a path that is symbolically linked to a different on-disk location, Node.js will dereference the link and use the actual on-disk "real path" of the module as both an identifier and as a root path to locate other dependency modules. In most cases, this default behavior is acceptable. However, when using symbolically linked peer dependencies, as illustrated in the example below, the default behavior causes an exception to be thrown if moduleA attempts to require moduleB as a peer dependency:

{appDir} ├── app │   ├── index.js │   └── node_modules │       ├── moduleA -> {appDir}/moduleA │       └── moduleB │           ├── index.js │           └── package.json └── moduleA     ├── index.js     └── package.json

The --preserve-symlinks command line flag instructs Node.js to use the symlink path for modules as opposed to the real path, allowing symbolically linked peer dependencies to be found.

Note, however, that using --preserve-symlinks can have other side effects. Specifically, symbolically linked native modules can fail to load if those are linked from more than one location in the dependency tree (Node.js would see those as two separate modules and would attempt to load the module multiple times, causing an exception to be thrown).

--track-heap-objects#

Added in: v2.4.0

Track heap object allocations for heap snapshots.

--prof-process#

Added in: v6.0.0

Process v8 profiler output generated using the v8 option --prof.

--v8-options#

Added in: v0.1.3

Print v8 command line options.

Note: v8 options allow words to be separated by both dashes (-) or underscores (_).

For example, --stack-trace-limit is equivalent to --stack_trace_limit.

--tls-cipher-list=list#

Added in: v4.0.0

Specify an alternative default TLS cipher list. (Requires Node.js to be built with crypto support. (Default))

--enable-fips#

Added in: v6.0.0

Enable FIPS-compliant crypto at startup. (Requires Node.js to be built with ./configure --openssl-fips)

--force-fips#

Added in: v6.0.0

Force FIPS-compliant crypto on startup. (Cannot be disabled from script code.) (Same requirements as --enable-fips)

--openssl-config=file#

Added in: v6.9.0

Load an OpenSSL configuration file on startup. Among other uses, this can be used to enable FIPS-compliant crypto if Node.js is built with./configure --openssl-fips.

--icu-data-dir=file#

Added in: v0.11.15

Specify ICU data load path. (overrides NODE_ICU_DATA)

Environment Variables#

NODE_DEBUG=module[,…]#

Added in: v0.1.32

‘,‘-separated list of core modules that should print debug information.

NODE_PATH=path[:…]#

Added in: v0.1.32

‘:‘-separated list of directories prefixed to the module search path.

Note: on Windows, this is a ‘;‘-separated list instead.

NODE_DISABLE_COLORS=1#

Added in: v0.3.0

When set to 1 colors will not be used in the REPL.

NODE_ICU_DATA=http://www.mamicode.com/file#

Added in: v0.11.15

Data path for ICU (Intl object) data. Will extend linked-in data when compiled with small-icu support.

NODE_REPL_HISTORY=file#

Added in: v3.0.0

Path to the file used to store the persistent REPL history. The default path is ~/.node_repl_history, which is overridden by this variable. Setting the value to an empty string ("" or " ") disables persistent REPL history.

NODE_TTY_UNSAFE_ASYNC=1#

Added in: 6.4.0

When set to 1, writes to stdout and stderr will be non-blocking and asynchronous when outputting to a TTY on platforms which support async stdio. Setting this will void any guarantee that stdio will not be interleaved or dropped at program exit. Use of this mode is not recommended.

NODE_EXTRA_CA_CERTS=file#

When set, the well known "root" CAs (like VeriSign) will be extended with the extra certificates in file. The file should consist of one or more trusted certificates in PEM format. A message will be emitted (once) with process.emitWarning() if the file is missing or misformatted, but any errors are otherwise ignored.

Note that neither the well known nor extra certificates are used when the ca options property is explicitly specified for a TLS or HTTPS client or server.

Example#

An example of a web server written with Node.js which responds with ‘Hello World‘:

const http = require(‘http‘);const hostname = ‘127.0.0.1‘;const port = 3000;const server = http.createServer((req, res) => {  res.statusCode = 200;  res.setHeader(‘Content-Type‘, ‘text/plain‘);  res.end(‘Hello World\n);});server.listen(port, hostname, () => {  console.log(`Server running at http://${hostname}:${port}/`);});

To run the server, put the code into a file called example.js and execute it with Node.js:

$ node example.jsServer running at http://127.0.0.1:3000/

All of the examples in the documentation can be run similarly.

Node.js-Usage & Example