DEV Community

Cover image for Part 2: Setting Up Your Node.js Environment
Dipak Ahirav
Dipak Ahirav

Posted on • Edited on

Part 2: Setting Up Your Node.js Environment

๐Ÿš€ 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. 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. 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:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

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.
   mkdir my-node-project
   cd my-node-project
Enter fullscreen mode Exit fullscreen mode
  1. 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.
   npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. 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:

   touch index.js
   mkdir lib
Enter fullscreen mode Exit fullscreen mode

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:

console.log('Hello, Node.js!');
Enter fullscreen mode Exit fullscreen mode

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

node index.js
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (1)

Collapse
 
dipakahirav profile image
Dipak Ahirav

Next part -> PART - 3