Home > Web Front-end > JS Tutorial > body text

Installing And Running NPM Packages Using The Terminal

Barbara Streisand
Release: 2024-11-07 05:54:02
Original
936 people have browsed it

When working on a JavaScript project, chances are you'll need to install and use npm (Node Package Manager) packages.

npm is essential for managing libraries and tools that enhance your development process. If you're new to this, here's a step-by-step guide to installing and running npm packages using the terminal.

Prerequisites
Install Node.js: Ensure that the recent version of Node.js is installed on your machine, as npm comes bundled with it. You can check if it's installed by running:

  • node -v

and

  • npm -v

Installing And Running NPM Packages Using The Terminal

If you don't have it installed, download it from https://nodejs.org/https://nodejs.org/ and follow the installation instructions.

After confirming it is successfully installed, create a folder for your project or navigate to an existing one.

Initialize The Project

To start using npm, you need a package.json file, which tracks the project's dependencies and scripts. You can create one by running:

  • npm init

Installing And Running NPM Packages Using The Terminal

You’ll be prompted with some basic questions (like project name, version, description, etc.). You can either answer each or press Enter to accept the defaults.
Alternatively, you can use npm init -y to create a default package.json file without prompts.

Installing npm Packages

With npm, you can install packages either globally or locally to your project.

  • Local Installation: Installs the package in the project directory and adds it to your package.json.

npm install package-name

This installs the package locally. You'll find it under the node_modules folder, and it will be listed in your package.json file.

  • Global Installation: Installs the package globally on your system, making it available from any directory.

npm install -g package-name

This installs the package globally, allowing you to run the package commands directly in any project without needing to install it locally.

Using npm Scripts to Run Commands

Scripts defined in package.json can simplify common tasks like running tests or starting the server. In your package.json, there’s a "scripts" section where you can define custom commands.

Example:


"scripts": {
"start": "node index.js",
"test": "echo "Error: no test specified" && exit 1"
}

In this example:

"start" will run "node index.js".
You can replace "test" or "start" with other custom scripts.

To run a script, use:

npm run script-name

Running Installed Packages from the Terminal

If you installed a package globally, you can use it directly in the terminal.
For locally installed packages, use npx, which comes bundled with npm:
npx package-name

Example:

npx create-react-app my-app

This command runs the create-react-app package without needing to install it globally.

Updating and Removing Packages

To update a package, use:
npm update package-name

To remove a package, use:

npm uninstall package-name

This command will remove the package from the node_modules folder and from package.json.

Conclusion
By understanding these basic npm commands, you can confidently install, run, and manage packages in any Node.js project.

The above is the detailed content of Installing And Running NPM Packages Using The Terminal. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!