Home >Web Front-end >JS Tutorial >Installing And Running NPM Packages Using The Terminal
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:
and
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:
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.
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.
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!