ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
2009年,美国程序员Ryan Dahl创造了node.js项目,将javascript语言用于服务器端编程。 [node.js官方网站](https://nodejs.org/en/) [npm.js官方网站](https://www.npmjs.com/) Node.js® is * a JavaScript runtime built on [Chrome's V8 JavaScript engine](https://developers.google.com/v8/). * an asynchronous event driven JavaScript runtime, Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform. This behavior is like browser JavaScript — the event loop is hidden from the user. HTTP is a first class citizen in Node, designed with streaming and low latency in mind. This makes Node well suited for the foundation of a web library or framework. Just because Node is designed without threads, doesn't mean you cannot take advantage of multiple cores in your environment. Child processes can be spawned by using our [`child_process.fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options) API, and are designed to be easy to communicate with. Built upon that same interface is the [`cluster`](https://nodejs.org/api/cluster.html) module, which allows you to share sockets between processes to enable load balancing over your cores. [TOC] ## Node version managers Node version managers allow you to install and switch between multiple versions of Node.js and npm on your system so you can test your applications on multiple versions of npm to ensure they work for users on different versions. ### OSX or Linux Node version managers [[官方网站]](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm#osx-or-linux-node-version-managers) * [nvm](https://github.com/creationix/nvm) * [n](https://github.com/tj/n) ### Windows Node version managers [[官方网站]](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm#windows-node-version-managers) * [nodist](https://github.com/marcelklehr/nodist) * [nvm-windows](https://github.com/coreybutler/nvm-windows) `nvm-windows`(NVM for Windows) is a command line tool. Simply type`nvm`in the console for help. The basic commands are: * `nvm arch [32|64]`: Show if node is running in 32 or 64 bit mode. Specify 32 or 64 to override the default architecture. * `nvm install <version> [arch]`: The version can be a node.js version or "latest" for the latest stable version. Optionally specify whether to install the 32 or 64 bit version (defaults to system arch). Set`[arch]`to "all" to install 32 AND 64 bit versions. * `nvm list [available]`: List the node.js installations. Type`available`at the end to show a list of versions available for download. * `nvm on`: Enable node.js version management. * `nvm off`: Disable node.js version management (does not uninstall anything). * `nvm proxy [url]`: Set a proxy to use for downloads. Leave`[url]`blank to see the current proxy. Set`[url]`to "none" to remove the proxy. * `nvm uninstall <version>`: Uninstall a specific version. * `nvm use <version> [arch]`: Switch to use the specified version. Optionally specify 32/64bit architecture.`nvm use <arch>`will continue using the selected version, but switch to 32/64 bit mode based on the value supplied to`<arch>`. For information about using`use`in a specific directory (or using`.nvmrc`), please refer to[issue #16](https://github.com/coreybutler/nvm-windows/issues/16). * `nvm root <path>`: Set the directory where nvm should store different versions of node.js. If`<path>`is not set, the current root will be displayed. * `nvm version`: Displays the current running version of NVM for Windows. * `nvm node_mirror <node_mirror_url>`: Set the node mirror.People in China can use*[https://npm.taobao.org/mirrors/node/](https://npm.taobao.org/mirrors/node/)* * `nvm npm_mirror <npm_mirror_url>`: Set the npm mirror.People in China can use*[https://npm.taobao.org/mirrors/npm/](https://npm.taobao.org/mirrors/npm/)* ## Checking your version of npm and Node.js To see if you already have Node.js and npm installed and check the installed version, run the following commands: ~~~bash node -v npm -v ~~~ ## Module [[latest-v10.x-Module]](https://nodejs.org/dist/latest-v10.x/docs/api/modules.html#modules_modules) In the Node.js module system, each file is treated as a separate module. The module system is implemented in the `require('module')` module. ### attention 1 Functions and objects are added to the root of a module by specifying additional properties on the special `exports` object. Variables local to the module will be private, because the module is wrapped in a function by Node.js. **circle.js** ~~~js const { PI } = Math; exports.area = (r) => PI * r ** 2; exports.circumference = (r) => 2 * PI * r; ~~~ **foo.js** ~~~js const circle = require('./circle.js'); console.log(`The area of a circle of radius 4 is ${circle.area(4)}`); ~~~ The module `circle.js` has private variable `PI` and exported the functions `area()`, `circumference()` . ### attention 2 Use `module.exports` to modify module. **square.js** ~~~js // assigning to exports will not modify module, must use module.exports module.exports = class Square { constructor(width) { this.width = width; } area() { return this.width ** 2; } }; ~~~ **bar.js** ~~~js const Square = require('./square.js'); const mySquare = new Square(2); console.log(`The area of mySquare is ${mySquare.area()}`); ~~~ `bar.js` makes use of the `square` module, which exports a Square class: ### Core Modules Node.js has several modules compiled into the binary. These modules are described in greater detail elsewhere in this documentation. The core modules are defined within Node.js's source and are located in the `lib/` folder. Core modules are always preferentially loaded if their identifier is passed to `require()`. For instance, `require('http')` will always return the built in HTTP module, even if there is a file by that name. ### File Modules ### Folders as Modules It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to that library. There are three ways in which a folder may be passed to `require()` as an argument. 1. load `package.json` file The first is to create a `package.json` file in the root of the folder, which specifies a `main` module. An example `package.json` file might look like this: ~~~json { "name" : "some-library", "main" : "./lib/some-library.js" } ~~~ If this was in a folder at `./some-library`, then `require('./some-library')` would attempt to load `./some-library/lib/some-library.js`. This is the extent of Node.js's awareness of `package.json` files. 2. load `index.js` or `index.node` file If there is no `package.json` file present in the directory, or if the `'main'` entry is missing or cannot be resolved, then Node.js will attempt to load an `index.js` or `index.node` file out of that directory. For example, if there was no `package.json` file in the above example, then `require('./some-library')` would attempt to load: * `./some-library/index.js` * `./some-library/index.node` If these attempts fail, then Node.js will report the entire module as missing with the default error: ~~~txt Error: Cannot find module 'some-library' ~~~