To set up a Node.js development environment, follow these steps:
-
Install Node.js:
- Go to the official Node.js website: https://nodejs.org/
- Download the LTS (Long Term Support) version for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the prompts to complete the installation.
-
Verify Installation:
- Open your terminal or command prompt.
- Type
node -vand press Enter. This command will display the installed Node.js version. - Type
npm -vand press Enter. This command will display the installed npm (Node Package Manager) version.
-
Set Up a Project Directory:
- Create a new directory for your Node.js project using the command:
mkdir my-nodejs-project - Navigate into the project directory:
cd my-nodejs-project
- Create a new directory for your Node.js project using the command:
-
Initialize a New Node.js Project:
- Run the following command to create a
package.jsonfile, which will manage your project's dependencies and scripts:npm init -y - This command creates a default
package.jsonfile in your project directory.
- Run the following command to create a
-
Install a Code Editor:
- Download and install a code editor like Visual Studio Code (VS Code) from https://code.visualstudio.com/.
- Open your project directory in the code editor.
-
Install Additional Packages (Optional):
- You can install additional Node.js packages using npm. For example, to install the Express framework, run:
npm install express
- You can install additional Node.js packages using npm. For example, to install the Express framework, run:
-
Create Your First Node.js File:
- Create a new file named
app.jsin your project directory. - Open
app.jsin your code editor and add the following code:console.log('Hello, Node.js!');
- Save the file.
- Create a new file named
-
Run Your Node.js Application:
- In your terminal, run the following command to execute your Node.js application:
node app.js - You should see the output
Hello, Node.js!in the terminal.
- In your terminal, run the following command to execute your Node.js application:
Congratulations! You have successfully set up a Node.js development environment. You can now start building your Node.js applications.