此内容没有您所选择的语言版本。
Chapter 8. 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, use:
# yum install npm nodejs
8.1. Creating a new JavaScript project
The following section describes how to create a new JavaScript project in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Click
. The New window appears.
- Enter JavaScript in the search field.
- Select JavaScript Project.
Click the
button.The Create a JavaScript project window appears.
Name your project.
All other fields are populated automatically.
Click the
button.The Open Associated Perspective window appears.
- Click the button.
Your newly created JavaScript project is now listed in the Project Explorer view.
8.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.
Click
. The New window appears.
- Enter npm in the search field.
- Select npm Init.
Click the
button.The npm Initialization Wizard window appears.
-
Click the
package.json
file. button to select the base directory for the Click the
button.The Open Associated Perspective window appears.
- Click the 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.
8.3. Creating a new .js
file
The following section describes how to create a new .js
file in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Click
. The Create New File window appears.
- Select a parent folder for the file.
Name the file.
For example
index.js
.- Click the button.
Your newly created .js
file is now displayed in the CodeReady Studio editor.
8.4. Importing an existing Node.js application
The following section describes how to import an existing Node.js application.
Procedure
- Start CodeReady Studio.
Click
. The Import window appears.
- Expand the General folder.
- Select Existing Project into Workspace.
- Click the button.
- Click the button to locate your existing project.
- Select the path to your project in the Projects field.
- Click the button.
Verification steps
Click
. The Open Perspective window appears.
- Select JavaScript.
Click the
button.The Project Explorer view appears.
Your newly imported Node.js application is now listed in the Project Explorer view.
8.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 Project Explorer.
-
Right-click target
.
The Console view appears running your Node.js application.
8.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
. The Console view appears, displaying the error log.