转一篇关于 NPM 的介绍(Nodejs相关), 英文
转一篇关于 NPM 的介绍(Nodejs相关), 英文
http://tech.pro/tutorial/1190/package-managers-an-introductory-guide-for-the-uninitiated-front-end-developer Package Managers: An Introductory Guide For The Uninitiated Front-End Developer
标题太长… 自己看, 复制一下目录
What is a Package Manager? What is npm? Installing npm (and Node.js) Configuring npm Updating npm Uninstalling npm Where can I find npm documentation? Searching the npm registry Installing and Updating packages from the npm registry Installing Packages locally Installing packages globally Updating packages Listing npm packages Uninstalling npm packages package.json file Package details are stored in package.json Viewing package.json before installing Interactively create a package.json file NPM is great, but I am a front-end engineer and NPM is for backend JS developers
Introduction to NPM (Node Package Manager)
What is a Package Manager?
A package manager is a tool that helps manage software dependencies. It simplifies the process of installing, updating, configuring, and removing software packages.
What is npm?
npm stands for Node Package Manager. It is the default package manager for Node.js. npm enables developers to install, share, and distribute code as well as reuse existing solutions for common tasks.
Installing npm (and Node.js)
To get started with npm, you need to first install Node.js, which includes npm by default. You can download it from the official website:
# For Windows or macOS
https://nodejs.org/en/download/
# For Linux
sudo apt-get install nodejs
After installation, you can verify the installation by running:
node -v
npm -v
This will print out the installed versions of Node.js and npm.
Configuring npm
You can configure npm using the npm config
command. For example, to set a custom registry:
npm config set registry https://registry.npmjs.org/
Updating npm
To update npm to the latest version, use the following command:
npm install -g npm
Uninstalling npm
Since npm comes bundled with Node.js, uninstalling npm typically involves uninstalling Node.js. On Windows or macOS, you can simply remove the Node.js application. On Linux, you can use:
sudo apt-get remove nodejs
Where can I find npm documentation?
The npm documentation is available online at:
https://docs.npmjs.com/
Searching the npm Registry
To search for a package in the npm registry, use:
npm search <package-name>
For example:
npm search express
This will list all packages related to “express”.
Installing and Updating Packages from the npm Registry
Installing Packages Locally
To install a package locally (for your project):
npm install express
This creates a node_modules
directory in your project and installs the express
package there.
Installing Packages Globally
To install a package globally (available system-wide):
npm install -g express
Updating Packages
To update a local package:
npm update express
To update all packages:
npm update
Listing npm Packages
To list all installed packages locally:
npm list
To list all global packages:
npm list -g --depth=0
Uninstalling npm Packages
To uninstall a local package:
npm uninstall express
To uninstall a global package:
npm uninstall -g express
package.json File
The package.json
file is a configuration file for your project. It stores metadata about your project and its dependencies.
Package Details are Stored in package.json
Example package.json
:
{
"name": "my-app",
"version": "1.0.0",
"dependencies": {
"express": "^4.17.1"
}
}
Viewing package.json Before Installing
You can view the package.json
file before installing dependencies:
cat package.json
Interactively Create a package.json File
To create a package.json
file interactively:
npm init
This will guide you through creating a package.json
file step-by-step.
NPM is Great, But I am a Front-End Engineer and NPM is for Backend JS Developers
While NPM is commonly used for backend development, it’s also widely used in front-end projects. Many front-end libraries and tools (e.g., React, Vue, Webpack) have their own npm packages. Using npm can streamline your workflow and help manage dependencies effectively.
This introduction covers the basics of npm and how to use it effectively in both front-end and back-end projects.
Sure! Here is an introductory guide on npm (Node Package Manager) with examples and explanations.
What is a Package Manager?
A package manager is a tool that helps you manage software dependencies. It allows you to install, update, and uninstall packages easily. For Node.js, npm (Node Package Manager) is the most popular package manager.
What is npm?
npm is a command-line tool that comes bundled with Node.js. It allows you to install, update, and manage JavaScript libraries and tools.
Installing npm (and Node.js)
First, you need to install Node.js. npm will come installed with it. You can download Node.js from the official website:
# On Windows/Mac
https://nodejs.org/en/download/
# On Linux
sudo apt-get install nodejs
After installation, you can check if npm is installed by running:
npm -v
Configuring npm
You can configure npm using the npm config
command. For example, setting the registry URL:
npm config set registry https://registry.npmjs.org/
Updating npm
To update npm itself, use:
npm install -g npm
Uninstalling npm
Uninstalling npm is not usually necessary since it comes with Node.js. If you want to reinstall Node.js, you can remove the existing version first.
Where can I find npm documentation?
The npm documentation is available at:
Searching the npm Registry
To search for a package in the npm registry, use:
npm search <package-name>
Installing and Updating Packages from the npm Registry
Installing Packages Locally
Install a package locally for your project:
npm install <package-name>
This will add the package to your node_modules
directory and update the package.json
file.
Installing Packages Globally
Install a package globally so it’s available system-wide:
npm install -g <package-name>
Updating Packages
Update a specific package:
npm update <package-name>
Or update all packages:
npm update
Listing npm Packages
List all packages installed locally:
npm list
List all global packages:
npm list -g --depth=0
Uninstalling npm Packages
Uninstall a local package:
npm uninstall <package-name>
Uninstall a global package:
npm uninstall -g <package-name>
package.json File
package.json
is a configuration file that stores metadata about your project and its dependencies.
Package Details are Stored in package.json
It includes fields like name
, version
, description
, dependencies
, etc.
Viewing package.json Before Installing
Before installing any package, you can view the package.json
file to see the dependencies:
{
"name": "my-project",
"version": "1.0.0",
"dependencies": {
"express": "^4.17.1"
}
}
Interactively Create a package.json File
You can create a package.json
file interactively:
npm init
Follow the prompts to generate the file.
NPM is Great, But I Am a Front-End Engineer and NPM Is for Backend JS Developers
While npm is primarily used for backend development, it is also widely used in front-end projects for managing libraries and tools like React, Vue, or Webpack. Many front-end frameworks and tools have CLI commands that rely on npm.
I hope this guide helps you understand npm better!