Getting Started with JavaScript on macOS

Setting up JavaScript development on macOS is simple using Node.js and Visual Studio Code. Follow this guide to get up and running quickly.


Step 1: Install Node.js

Node.js allows you to run JavaScript outside the browser. To install it on macOS:

  1. Go to the official Node.js site: https://nodejs.org
  2. Download the macOS Installer (LTS version recommended).
  3. Open the downloaded `.pkg` file and follow the installation instructions.
Node.js download page on macOS

Step 2: Verify Installation

Once installed, check that Node.js and npm are working:

  1. Open the Terminal app (found in Applications > Utilities).
  2. Run the following commands:
    node --version
    npm --version
  3. If you see version numbers, the installation was successful.

If Node is correctly installed, you will see the version of Node displayed in the terminal, something like:

For Node.js version:

v22.x.x

For npm version:

10.x.x

Step 3: Install Visual Studio Code

Visual Studio Code (VS Code) is a lightweight editor perfect for JavaScript development.

  1. Visit https://code.visualstudio.com
  2. Download and install the macOS version.
  3. Drag the VS Code app to your Applications folder, then open it.
Visual Studio Code homepage

Step 4: Write and Run Your First JavaScript Program

  1. Create a new file in VS Code and name it app.js.
  2. Add the following code:
    javascript
    console.log("Hello, JavaScript!");
  3. Save the file to a location like your Desktop.
  4. Open Terminal, navigate to the file's directory:
    cd ~/Desktop
  5. Run the script using:
    node app.js
    Running Node.js script in Terminal on macOS
  6. Alternatively, click the ▶️ Run button in VS Code to execute it.VS Code run button
  7. You should see this output:
    Hello, JavaScript!
  8. 🎉 You're all set to start building with JavaScript on macOS!

That’s it! You’ve successfully set up a JavaScript development environment on your Mac. Happy coding!


Frequently Asked Questions

Do I need to restart my Mac after installing Node.js?

Not usually. However, if the terminal doesn’t recognize node or npm commands after installation, try restarting your Mac to refresh the environment variables.


What is Node.js and why do I need it on macOS?

Node.js is a JavaScript runtime that allows you to run JavaScript code outside the browser. It's essential for developing JavaScript applications on your Mac.


How do I check if Node.js is installed on macOS?

Open the Terminal and type node --version and npm --version. If you see version numbers, Node.js and npm are installed correctly.


Can I use another code editor instead of VS Code on macOS?

Yes, you can use any code editor you prefer, such as Sublime Text or Atom. However, VS Code is highly recommended due to its features, extensions, and macOS integration.


How do I run JavaScript files from the terminal on macOS?

Navigate to the folder where your JavaScript file is saved using the cd command and run it with node filename.js.


What if node is not recognized as a command on macOS?

This typically means that Node.js wasn't added to your PATH. Reinstall Node.js and ensure the 'Add to PATH' option is checked during setup.