diff --git a/notes/js/js.md b/notes/js/js.md index b55e14412c30cc4f8ac1f7ac93ab9436f44f5433..0eb7ea2fa7be8c469a4322f065287af30be64f79 100644 --- a/notes/js/js.md +++ b/notes/js/js.md @@ -30,7 +30,6 @@ - [Running programs in Node.js](#running-the-interpreter) - [Modules](#modules): Node.js builtin modules, `require`, and `module.exports` - [Example](#nodejs-example) - - [npm](#installing-npm-packages): using open-source libraries with npm - [Documentation](#nodejs-documentation) --- @@ -907,7 +906,6 @@ list234.toArray() // [2, 3, 4] Most of the functions included in Node.js need to be imported from the corresponding library, e.g. `fs` for file system operations, `http` for making HTTP servers and requests, and `zlib` for compressing and decompressing files. See the example below for using one of these modules. -[npm packages](#installing-npm-packages) are also imported the same way. ### Node.js example @@ -940,43 +938,6 @@ fs.readFile(source, (err, data) => { }) ``` -### Installing npm packages - -If you haven't used a package manager before, the idea is simple: they allow people to publish open source code libraries for other projects to use. -Node.js's package manager is called [npm](npmjs.com) and it hosts more published packages than any other package manager. -If you want to use npm with a new project, run `npm init` to create a `package.json` file: this file stores information about the project, including which packages it depends on. -However, we will provide you all the `package.json` files you need. -To install the "dependencies" specified in `package.json`, run `npm install`. - -For example, the [`colors`](https://www.npmjs.com/package/colors) package on npm allows you to print colored text in the terminal. -You could use the following `package.json` for a new project `colors-test` that depends on version 1.4.0 or newer of `colors`: -```json -{ - "name": "colors-test", - "version": "0.0.1", - "dependencies": { - "colors": "^1.4.0" - } -} -``` -Running `npm install` installs `colors` and prints: -``` -added 1 package from 2 contributors and audited 1 package in 0.323s -found 0 vulnerabilities -``` -Now we can `require('colors')` in our program: -```js -const colors = require('colors') -console.log( - colors.red('r') + - colors.yellow('y') + - colors.green('g') + - colors.blue('b') -) -``` -Running this program prints: - - ### Node.js documentation Node.js also has very detailed documentation of its standard library at [nodejs.org](https://nodejs.org/api/).