Part 2: Setting Up Your Node.js Environment
Tags: node, javascript, beginners
Published: 2024-04-30 02:51:29

## 🚀 Check Out My YouTube Channel! 🚀
Hi everyone! If you enjoy my content here on Dev.to, please consider subscribing to my YouTube channel [devDive with Dipak](https://www.youtube.com/@DevDivewithDipak). I post practical full-stack development videos that complement my blog posts. Your support means a lot!

### Part 2: Setting Up Your Node.js Environment

In the first part of our Node.js series, we introduced you to what Node.js is and why it's a game-changer for modern web development. Now, let's roll up our sleeves and set up your Node.js environment. This guide will walk you through installing Node.js and npm, setting up a basic project structure, and writing your first Node.js script.

#### Step 1: Installing Node.js and npm

**Node.js** can be easily installed from its [official website](https://nodejs.org/). Depending on your operating system, you can choose the Windows, macOS, or Linux version. Node.js packages come with **npm** (Node Package Manager), which is essential for managing dependencies in your projects.

- **Windows and macOS Users**: Download the installer from the Node.js website and follow the prompts. Make sure that npm is selected to be installed along with Node.js.
- **Linux Users**: You can install Node.js and npm using your package manager. For most distributions, you can use commands like `sudo apt-get install nodejs npm` for Debian-based distributions or `sudo yum install nodejs npm` for Red Hat-based distributions.

After installation, you can verify the installation by opening your terminal or command prompt and typing:

```bash
node -v
npm -v
```

These commands should return the versions of Node.js and npm installed on your system, confirming a successful installation.

#### Step 2: Setting Up a Basic Project Structure

A well-organized project structure is crucial for managing complex applications. Let's start by setting up a simple project:

1. **Create a New Directory**: This will be your project folder.
   
   ```bash
   mkdir my-node-project
   cd my-node-project
   ```

2. **Initialize a New npm Project**: Run the following command and follow the prompts to create a `package.json` file, which will keep track of your project's dependencies and metadata.

   ```bash
   npm init -y
   ```

3. **Create a Basic File Structure**:

   - **`index.js`**: Your main application file.
   - **`lib`** (optional): A directory for your custom modules.
   - **`node_modules`** (automatically created by npm): Where your project’s dependencies are stored.

   You can create these using your text editor or the command line:

   ```bash
   touch index.js
   mkdir lib
   ```

#### Step 3: Writing Your First Node.js Script

Now, let’s write a simple script to verify everything is set up correctly. Open `index.js` and add the following code:

```javascript
console.log('Hello, Node.js!');
```

To run this script, return to your terminal and execute:

```bash
node index.js
```

If you see `Hello, Node.js!` printed to your console, congratulations! You have successfully set up your Node.js environment and run your first script.

#### Conclusion

You now have a functional Node.js environment and are ready to start building more complex applications. In the next part of our series, we will dive into asynchronous programming in Node.js, exploring callbacks, promises, and the async/await syntax.

Stay tuned, and happy coding!