Chapter 4. Node.js basics in CodeReady Studio

Node.js is an event-based, asynchronous I/O framework and is used to develop applications that run JavaScript on the client and server side. This allows the application to reuse parts of the code and to avoid switching contexts. Node.js is commonly used to create applications such as static file servers, messaging middleware, HTML5 game servers, web application framework, and others.

CodeReady Studio supports Node.js application development using the npm package installer and offers a built-in debugging tool to identify and fix issues with applications.

The following section describes how to:

  • Create a new JavaScript project.
  • Create a new package.js file.
  • Create a new JavaScript file.
  • Import an existing JavaScript project.
  • Run a Node.js application.

Prerequisites

  • Installed npm.
  • Installed Node.js.

    To install the prerequisites, on RHEL 7, use:

    # yum install npm nodejs

    To install the prerequisites, on RHEL 8, use:

    # dnf install npm nodejs

4.1. Creating a new JavaScript project

The following section describes how to create a new JavaScript project in CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Click FileNewOther.

    The New window appears.

  4. Enter JavaScript in the search field.
  5. Select JavaScript Project.
  6. Click the Next button.

    The Create a JavaScript project window appears.

  7. Name your project.

    All other fields are populated automatically.

  8. Click the Finish button.

    The Open Associated Perspective window appears.

  9. Click the Open Perspective button.

Your newly created JavaScript project is now listed in the Project Explorer view.

4.2. Creating a package.js file

The following section describes how to create a new package.json file in CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Click FileNewOther.

    The New window appears.

  4. Enter npm in the search field.
  5. Select npm Init.
  6. Click the Next button.

    The npm Initialization Wizard window appears.

  7. Click the Browse Workspace button to select the base directory for the package.json file.
  8. Click the Finish button.

    The Open Associated Perspective window appears.

  9. Click the Open Perspective button.

Your newly created package.js file is now displayed in the CodeReady Studio editor.

Additional resources

4.3. Creating a new .js file

The following section describes how to create a new .js file CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Click FileNewFile.

    The Create New File window appears.

  4. Select a parent folder for the file.
  5. Name the file.

    For example index.js.

  6. Click the Finish button.

Your newly created .js file is now displayed in the CodeReady Studio editor.

4.4. Importing an existing Node.js application

The following section describes how to import an existing Node.js application.

Procedure

  1. Start CodeReady Studio.
  2. Click FileImport.

    The Import window appears.

  3. Expand the General folder.
  4. Select Existing Project into Workspace.
  5. Click the Next button.
  6. Click the Browse button to locate your existing project.
  7. Select the path to your project in the Projects field.
  8. Click the Finish button.

Verification steps

  1. Click WindowPerspectiveOpen PerspectiveOther.

    The Open Perspective window appears.

  2. Select JavaScript.
  3. Click the Open button.

    The Project Explorer view appears.

Your newly imported Node.js application is now listed in the Project Explorer view.

4.5. Running a Node.js application

The following section describes how to run a Node.js application in CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Open Git Perspective.
  3. Open Project Explorer.
  4. Right-click target projectRun asNode.js Application.

The Console opens running your Node.js application.

4.6. Debugging a Node.js application

CodeReady Studio includes a debugger to help identify and resolve the issues.

The following section describes how to debug Node.js applications in CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Open Project Explorer.
  3. Expand the target project.
  4. Right-click the index.jsDebug asNode Program.

    The Console appears, displaying the error log.