npm (Node Package Manager) is a package manager for JavaScript, used to install and manage software packages. Here's a basic guide on how to use it:
npm -v
. If you see a version number, npm is installed.npm install <package-name>
: Installs a package and saves it to the package-json
file in your project's root directory.npm install express
installs the popular web framework express
.npm install <package-name>@<version>
: Installs a specific version of a package.npm install [email protected]
installs version 4.17.1 of express
.npm install -g <package-name>
: Installs a package globally, making it available system-wide.npm install -g typescript
installs the TypeScript compiler globally.npm update <package-name>
: Updates a package to its latest version.npm uninstall <package-name>
: Removes a package from your project.npm list
: Lists all installed packages.npm outdated
: Shows outdated packages in your project.npm run <script-name>
: Runs a script defined in the package.json
file.package.json
Filenpm init
: Initializes an empty package.json
file in your project's root directory. It will prompt you for basic project information.npm init -y
: Initializes a package.json
file with default settings.package.json
: This file stores information about your project, including dependencies.dependencies
: Packages required for your project to run. These are listed in the package.json
file.devDependencies
: Packages used during development but not required in production. These are also listed in the package.json
file.scripts
: The package.json
file allows you to define scripts that can be run using npm run
.{
"scripts": {
"start": "node index.js",
"build": "webpack --mode production"
}
}
You can then run these scripts using:
npm run start
npm run build
npm install
, it uses the package.json
file to determine which packages to install.package.json
doesn't exist, npm will create one for you.npm
with yarn
While npm is widely used, another popular package manager called yarn
is also available. yarn
is known for its speed and deterministic builds. You can choose to use either one, but it's generally recommended to stick to one for consistency within your project.
This tutorial provides a basic introduction to npm. There are many more advanced features and concepts to explore. As you work with npm, you'll encounter more specific commands and use cases based on your needs.