npm的快速指南,這是功能強大的包管理器,它是Node.js成功的關鍵。在2017年1月,據報導在npm註冊表中列出了超過350000個軟件包,這使其成為地球上最大的單一語言代碼存儲庫,並且您可以確定有(幾乎!)所有內容的軟件包。
npm簡介
npm
是Node.js的標準軟件包管理器。
在2017年1月,據報導在npm註冊表中列出了超過350000個軟件包,這使其成為地球上最大的單一語言代碼存儲庫,並且您可以確定有(幾乎!)所有內容的軟件包。
它起初是一種下載和管理依賴項的方式Node.js軟件包,但此後它已成為前端中使用的工具JavaScript。
有很多事情npm
做。
紗是npm的替代方法。確保您也將其簽出。
安裝
npm
是在安裝Node.js時安裝的。前往https://nodejs.org並安裝Node(如果尚未在系統上安裝它)。
如何使用npm
npm
管理項目依賴項的下載。
安裝所有依賴項
如果一個項目有一個packages.json
文件,通過運行
npm install
它將安裝項目所需的一切,node_modules
文件夾,如果尚不存在,則創建它。
安裝單個軟件包
您還可以通過運行以下命令來安裝特定的軟件包
npm install <package-name>
通常,您會在此命令中看到更多標誌:
--save
安裝並將條目添加到package.json
文件依存關係(默認為npm 5)--save-dev
安裝並將條目添加到package.json
文件devDependencies
區別主要在於devDependencies通常是開發工具,例如測試庫,而dependencies
已與生產中的應用捆綁在一起。
更新包
通過運行,更新也變得很容易
npm updatenpm
will check all packages for a newer version that satisfies your versioning constraints.
You can specify a single package to update as well:
npm update <package-name>Versioning
In addition to plain downloads, npm
also manages versioning, so you can specify any specific version of a package, or require a version higher or lower than what you need.
Many times you’ll find that a library is only compatible with a major release of another library.
Or a bug in the latest release of a lib, still unfixed, is causing an issue.
Specifying an explicit version of a library also helps to keep everyone on the same exact version of a package, so that the whole team runs the same version until the package.json
file is updated.
In all those cases, versioning helps a lot, and npm
follows the semantic versioning (semver) standard.
Running Tasks
The package.json file supports a format for specifying command line tasks that can be run by using
npm run <task-name>For example:
{
"scripts": {
"start-dev": "node lib/server-development",
"start": "node lib/server-production"
},
}
It’s very common to use this feature to run Webpack:
{
"scripts": {
"watch": "webpack --watch --progress --colors --config webpack.conf.js",
"dev": "webpack --progress --colors --config webpack.conf.js",
"prod": "NODE_ENV=production webpack -p --config webpack.conf.js",
},
}
So instead of typing those long commands, which are easy to forget or mistype, you can run
$ npm run watch
$ npm run dev
$ npm run prod
Download my free Node.js Handbook
More node tutorials:
- An introduction to the npm package manager
- Introduction to Node.js
- HTTP requests using Axios
- Where to host a Node.js app
- Interact with the Google Analytics API using Node.js
- The npx Node Package Runner
- The package.json guide
- Where does npm install the packages?
- How to update Node.js
- How to use or execute a package installed using npm
- The package-lock.json file
- Semantic Versioning using npm
- Should you commit the node_modules folder to Git?
- Update all the Node dependencies to their latest version
- Parsing JSON with Node.js
- Find the installed version of an npm package
- Node.js Streams
- Install an older version of an npm package
- Get the current folder in Node
- How to log an object in Node
- Expose functionality from a Node file using exports
- Differences between Node and the Browser
- Make an HTTP POST request using Node
- Get HTTP request body data using Node
- Node Buffers
- A brief history of Node.js
- How to install Node.js
- How much JavaScript do you need to know to use Node?
- How to use the Node.js REPL
- Node, accept arguments from the command line
- Output to the command line using Node
- Accept input from the command line in Node
- Uninstalling npm packages with `npm uninstall`
- npm global or local packages
- npm dependencies and devDependencies
- The Node.js Event Loop
- Understanding process.nextTick()
- Understanding setImmediate()
- The Node Event emitter
- Build an HTTP Server
- Making HTTP requests with Node
- The Node fs module
- HTTP requests in Node using Axios
- Reading files with Node
- Node File Paths
- Writing files with Node
- Node file stats
- Working with file descriptors in Node
- Working with folders in Node
- The Node path module
- The Node http module
- Using WebSockets with Node.js
- The basics of working with MySQL and Node
- Error handling in Node.js
- The Pug Guide
- How to read environment variables from Node.js
- How to exit from a Node.js program
- The Node os module
- The Node events module
- Node, the difference between development and production
- How to check if a file exists in Node.js
- How to create an empty file in Node.js
- How to remove a file with Node.js
- How to get the last updated date of a file using Node.js
- How to determine if a date is today in JavaScript
- How to write a JSON object to file in Node.js
- Why should you use Node.js in your next project?
- Run a web server from any folder
- How to use MongoDB with Node.js
- Use the Chrome DevTools to debug a Node.js app
- What is pnpm?
- The Node.js Runtime v8 options list
- How to fix the "Missing write access" error when using npm
- How to enable ES Modules in Node.js
- How to spawn a child process with Node.js
- How to get both parsed body and raw body in Express
- How to handle file uploads in Node.js
- What are peer dependencies in a Node module?
- How to write a CSV file with Node.js
- How to read a CSV file with Node.js
- The Node Core Modules
- Incrementing multiple folders numbers at once using Node.js
- How to print a canvas to a data URL
- How to create and save an image with Node.js and Canvas
- How to download an image using Node.js
- How to mass rename files in Node.js
- How to get the names of all the files in a folder in Node
- How to use promises and await with Node.js callback-based functions
- How to test an npm package locally
- How to check the current Node.js version at runtime
- How to use Sequelize to interact with PostgreSQL
- Serve an HTML page using Node.js
- How to solve the `util.pump is not a function` error in Node.js