
What is npm?
What is npm? 관련
npm (Node Package Manager) is the default package manager for JavaScript and Node.js. It allows developers to install, share, and manage libraries or dependencies that make building applications easier and more efficient.
npm provides access to a vast ecosystem of open-source packages hosted on the npm registry, making it one of the largest software repositories in the world.
npm comes bundled with Node.js, meaning that once you install Node.js, you automatically have access to npm. You can check if npm is installed by running the following command in your terminal:
npm -v
This command should return the version of npm installed on your system.
How npm Works
npm operates through three key components:
- The npm Registry – A public repository that hosts open-source JavaScript packages.
- The npm CLI (Command Line Interface) – A tool that allows developers to install, update, and manage packages from the command line.
- The package.json File – A metadata file that keeps track of dependencies, scripts, and project configurations.
When you install a package using npm, it pulls the package from the registry and saves it in the node_modules
folder within your project.
For example, to install Lodash, a popular utility library, you would run:
npm install lodash
This will:
- Download the latest version of
lodash
from the npm registry - Add it to your
node_modules
folder - Update the
package.json
andpackage-lock.json
files to reflect the new dependency
The Role of package.json
The package.json
file is the heart of any npm project. It serves as a blueprint, containing information about the project, including:
- Project metadata (name, version, description)
- Dependencies (external packages required for the project)
- Scripts (commands to automate tasks like starting a server or running tests)
- Versioning information (ensuring compatibility between different versions of dependencies)
A typical package.json
file looks like this:
{
"name": "my-awesome-project",
"version": "1.0.0",
"description": "A sample project demonstrating npm usage",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"No tests specified\" && exit 0"
},
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"eslint": "^8.0.0"
},
"author": "Your Name",
"license": "MIT"
}
dependencies
– Lists essential packages required for the application to function.devDependencies
– Includes development-only dependencies (for example, testing and linting tools).scripts
– Defines CLI commands for automating tasks.
To install all dependencies listed in package.json
, simply run:
npm install
This ensures all required packages are downloaded and ready for use.
Key npm Commands
Here are some essential npm commands you’ll use frequently:
Command | Description |
---|---|
npm init -y | Creates a default package.json file |
npm install <package-name> | Installs a package and adds it to dependencies |
npm install <package-name> --save-dev | Installs a package and adds it to devDependencies |
npm uninstall <package-name> | Removes a package from the project |
npm update | Updates all installed dependencies |
npm outdated | Checks for outdated dependencies |
npm run <script-name> | Runs a script defined in package.json |