๐ 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 orsudo 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
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:
- Create a New Directory: This will be your project folder.
mkdir my-node-project
cd my-node-project
-
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
- 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
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!');
To run this script, return to your terminal and execute:
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!
Top comments (1)
Next part -> PART - 3