-
Language:
English
-
Language:
English
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
- Start CodeReady Studio.
- Open Git Perspective.
Click File → New → Other.
The New window appears.
- Enter JavaScript in the search field.
- Select JavaScript Project.
Click the Next button.
The Create a JavaScript project window appears.
Name your project.
All other fields are populated automatically.
Click the Finish button.
The Open Associated Perspective window appears.
- 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
- Start CodeReady Studio.
- Open Git Perspective.
Click File → New → Other.
The New window appears.
- Enter npm in the search field.
- Select npm Init.
Click the Next button.
The npm Initialization Wizard window appears.
-
Click the Browse Workspace button to select the base directory for the
package.json
file. Click the Finish button.
The Open Associated Perspective window appears.
- Click the Open Perspective button.
Your newly created package.js
file is now displayed in the CodeReady Studio editor.
Additional resources
-
For more information on how to handle npm
package.json
files, see https://docs.npmjs.com/files/package.json#dependencies.
4.3. Creating a new .js
file
The following section describes how to create a new .js
file CodeReady Studio.
Procedure
- Start CodeReady Studio.
- Open Git Perspective.
Click File → New → File.
The Create New File window appears.
- Select a parent folder for the file.
Name the file.
For example
index.js
.- 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
- Start CodeReady Studio.
Click File → Import.
The Import window appears.
- Expand the General folder.
- Select Existing Project into Workspace.
- Click the Next button.
- Click the Browse button to locate your existing project.
- Select the path to your project in the Projects field.
- Click the Finish button.
Verification steps
Click Window → Perspective → Open Perspective → Other.
The Open Perspective window appears.
- Select JavaScript.
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
- Start CodeReady Studio.
- Open Git Perspective.
- Open Project Explorer.
- Right-click target project → Run as → Node.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
- Start CodeReady Studio.
- Open Project Explorer.
- Expand the target project.
Right-click the index.js → Debug as → Node Program.
The Console appears, displaying the error log.