Step by step tutorial on creating your first Hello World App in Node To start Node.js development on your Mac laptop, you'll need to install some essential packages and applications. Here’s a comprehensive list along with the terminal commands to get everything set up:
Homebrew is a package manager for macOS that simplifies the installation of software.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Node.js is the runtime environment, and npm (Node Package Manager) comes with it.
brew install node
Check that Node.js and npm were installed correctly.
node -v
npm -v
Git is essential for version control and collaboration.
brew install git
You can use any text editor, but here are some popular ones:
- Visual Studio Code (VS Code): A popular editor for JavaScript development.
brew install --cask visual-studio-code
These packages will help streamline your development process:
- Express: A web framework for Node.js.
- Nodemon: Automatically restarts the server during development.
- dotenv: Loads environment variables from a
.env
file.
npm install express nodemon dotenv
Create a new directory for your project and navigate into it.
mkdir my-node-app
cd my-node-app
This command creates a package.json
file for your project, which keeps track of dependencies.
npm init -y
Create an index.js
file in your project folder:
touch index.js
Open index.js
in your text editor and add the following code:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Use Nodemon to start your server so it restarts automatically on changes.
npx nodemon index.js
Open your web browser and go to http://localhost:3000
. You should see "Hello, World!"
- Postman: A tool for testing APIs.
brew install --cask postman
- MongoDB: If you plan to work with databases, you might want to install MongoDB.
brew tap mongodb/brew
brew install mongodb-community
Here’s a summary of all the commands:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install node
node -v
npm -v
brew install git
brew install --cask visual-studio-code
npm install express nodemon dotenv
mkdir my-node-app
cd my-node-app
npm init -y
touch index.js
npx nodemon index.js
brew install --cask postman
brew tap mongodb/brew
brew install mongodb-community
This should give you a solid foundation for starting your Node.js development journey.