Basic Terminal Shell Commands for Working with Node.js and npm
Welcome to today's lecture on the basic terminal shell commands necessary for working with Node.js and npm. This session will cover the fundamental commands you need to create and manage Node.js projects efficiently.
#### 1. **Navigating the File System**
Understanding how to navigate the file system is crucial when working with Node.js projects. Here are some basic commands:
- **`mkdir`**: Creates a new directory.
```bash
mkdir projectdirectory
```
- **`cd`**: Changes the current directory.
```bash
cd projectdirectory
```
- **`dir` (Windows) / `ls` (Unix/Linux/MacOS)**: Lists the contents of a directory.
```bash
dir
```
#### 2. **Setting Up a Node.js Project**
Once you have navigated to your project directory, the next step is to set up your Node.js project.
- **`npm init`**: Initializes a new Node.js project and creates a `package.json` file.
```bash
npm init
```
During the initialization process, you will be prompted to provide information about your project, such as the name, version, description, entry point, test command, git repository, keywords, author, and license. You can press `Enter` to accept the default values or type your own.
#### 3. **Installing npm Packages**
To add functionality to your Node.js project, you'll need to install npm packages.
- **`npm install <package-name>`**: Installs a package and adds it to your `package.json` file.
```bash
npm install express
```
- **`npm install`**: Installs all dependencies listed in the `package.json` file.
```bash
npm install
```
#### 4. **Running Your Node.js Application**
To run your Node.js application, you typically use the `node` command followed by the entry point of your application.
- **`node <file-name>`**: Runs a Node.js application.
```bash
node server.js
```
If you've specified a different entry point in your `package.json` file, use that file name instead of `server.js`.
#### 5. **Managing npm Packages**
As you work on your project, you might need to update or uninstall packages.
- **`npm update`**: Updates all packages to their latest version based on the version ranges specified in `package.json`.
```bash
npm update
```
- **`npm uninstall <package-name>`**: Uninstalls a package and removes it from `package.json`.
```bash
npm uninstall express
```
#### 6. **Viewing npm Package Information**
Sometimes, you need to view detailed information about installed packages or those you intend to install.
- **`npm list`**: Lists installed packages in the current project.
```bash
npm list
```
- **`npm info <package-name>`**: Shows detailed information about a package.
```bash
npm info express
```
#### 7. **Working with Package Scripts**
You can define custom scripts in your `package.json` file to automate tasks. These scripts can be run using the `npm run` command.
- **Adding a Script**: In your `package.json`, you can define scripts under the "scripts" section.
```json
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
```
- **Running a Script**: Use the `npm run` command followed by the script name.
```bash
npm run start
```
#### 8. **Commonly Used npm Commands**
Here are some additional npm commands that are frequently used:
- **`npm init -y`**: Initializes a new project with default values.
```bash
npm init -y
```
- **`npm cache clean --force`**: Clears the npm cache.
```bash
npm cache clean --force
```
- **`npm outdated`**: Checks for outdated packages.
```bash
npm outdated
```
- **`npm prune`**: Removes extraneous packages.
```bash
npm prune
```
Summary
Understanding these basic terminal shell commands is essential for working effectively with Node.js and npm. Here’s a quick recap:
- Navigate and manage directories (`mkdir`, `cd`, `dir`/`ls`).
- Initialize and manage Node.js projects (`npm init`, `npm install`).
- Run applications (`node <file-name>`).
- Manage npm packages (`npm update`, `npm uninstall`).
- View package information (`npm list`, `npm info`).
- Work with package scripts (`npm run <script>`).
By mastering these commands, you'll be well-equipped to handle the setup, development, and management of your Node.js projects.