Installing dependencies listed in package.json in the current directory is commonly done using npm. The following steps outline the process:
-
Open the terminal: First, open the terminal. On Windows, it could be CMD or PowerShell; on Mac or Linux, it is typically Terminal.
-
Navigate to the project directory: Use the
cdcommand to navigate to the project directory containing thepackage.jsonfile. For example:bashcd path/to/your/projectVerify that the
package.jsonfile exists in this directory. -
Run npm install: In the project directory, run the following command to install all dependencies:
bashnpm installThis command reads the
package.jsonfile and installs the required npm packages based on the dependencies specified. -
Check node_modules: After installation, all dependencies will be placed in the
node_modulesfolder within the project directory. You can confirm the installation by inspecting this folder. -
Run the project: If the project includes a startup script, such as
npm start, you can run it to launch the project, ensuring all dependencies are correctly installed and configured.
Example:
Assume I have a Node.js project with the following structure:
shellmy-project/ ├── package.json └── src/ └── index.js
Where package.json specifies dependencies like express. Running npm install in the project directory will read the package.json file, downloading express and other dependencies to my-project/node_modules.
This method ensures that all developers can use the same version of dependencies across different environments, enhancing the project's portability and maintainability.