A step-by-step guide to connecting Node.js 20 to Oracle Database 23c for free using node-oracledb

Posted by

How to connect Node.js to Oracle Database using node-oracledb

Node.js is a popular and powerful runtime environment that allows you to run JavaScript on the server side. Oracle Database is a powerful and widely used relational database management system. In this article, we will look at how to connect Node.js to Oracle Database using the node-oracledb module, and we will do it for free using the Oracle Database 23c Free Edition.

Step 1: Install Node.js

If you haven’t already installed Node.js, you can download it from the official website here.

Step 2: Install Oracle Database 23c Free Edition

Oracle Database 23c Free Edition is a free-to-use, full-featured version of the Oracle Database, and it includes all the features and capabilities of the commercial version. You can download and install it from the official website here.

Step 3: Install node-oracledb

Node-oracledb is an open source, cross-platform module for accessing Oracle Database from Node.js. You can install it using npm, the Node.js package manager, by running the following command in your terminal:


npm install oracledb

Step 4: Configure Oracle Database

Once you have installed Oracle Database, you will need to configure it to allow remote connections. This can be done by opening the Oracle Net Manager and creating a listener using the hostname and port of your Oracle Database.

Step 5: Create a connection in Node.js

Now that you have Node.js, Oracle Database, and node-oracledb set up, you can create a connection to the database in your Node.js application. Here is an example of how to do it:


const oracledb = require('oracledb');
async function run() {
let connection;
try {
connection = await oracledb.getConnection({
user: "your_username",
password: "your_password",
connectString: "your_hostname/your_service_name",
});

// You can now use the "connection" to execute SQL queries against the Oracle Database

} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();

Step 6: Test the connection

Once you have set up the connection in your Node.js application, you can test it by running the application and executing some SQL queries against the Oracle Database.

That’s it! You have now successfully connected Node.js to Oracle Database using the node-oracledb module, and you have done it for free using the Oracle Database 23c Free Edition.

Happy coding!