First setup of a connection from Node.js to an Oracle Database

Jaap Poot 6
0 0
Read Time:4 Minute, 2 Second

In this article I will demonstrate how to make a connection to a remote Oracle database from Node.js running on linux 7. We will be using the node-oracledb module to accomplish this. Lucas Jellema gave a great explanation about this module in his recent article Running node-oracledb – the Oracle Database Driver for Node.js – in the Pre Built VM for Database Development

As described in that article, the node-oracldb module is depending on the Oracle 11.2 or 12.1 client libraries. So you need to install a full Oracle client, local database or the Oracle Instant Client. I will be using the Oracle Instant Client since it is small and easy to install.

Why Linux 7?

As of Node.js 4 the compiler must support C++11.
This is not included in the default compiler on Linux 6. You can either install another compiler or use Linux 7.


In this article I will be using the following setup.

Oracle Linux 7 VM on VirtualBox
Node.js 4.4.2 (64-bits)
node-oracledb 1.8
Oracle Instantclient

OS prerequisites


Use yum to install the OS prerequisites.

yum install unzip libaio gcc-c++

On another VM, I have an Oracle Database pluggable database running.

Download the components

node-v4.4.2-linux-x64.tar.xz from from Oracle OTN from Oracle OTN
node-oracledb will be installed via node package manager, npm

Put the files in the /tmp directory of the VM using any sftp tool you like.

We will remove them when we are done.

Installing components

Logon as root (or use sudo)

Install Node.js

cd /opt
tar -Jxf /tmp/node-v4.4.2-linux-x64.tar.xz

Install Oracle instant client

mkdir /opt/oracle
cd /opt/oracle
unzip -q /tmp/
unzip -q /tmp/

Rename the directory so we don’t have to tell the installer where to find the OCI libraries, etc…
If you install the Oracle Instant Client in another location, you will have to set two environment variables, OCI_LIB_DIR and OCI_INC_DIR before installing th oracledb module. See on Github for more details about this.

mv instantclient_12_1 instantclient
cd instantclient
ln -s

Remove files from /tmp

rm /tmp/instantclient-* node-v4.4.2-linux-x64.tar.xz

Install oracledb module

You can choose to install the module local to the user of global for the system.
If you install it local to the user, you don’t need to be a privileged user. You can choose any user you need to run Node.js.
For this demonstration I created a user called nodejs and will install the module local to the user

Logon as user nodejs

Set environment variables

export PATH=/opt/node-v4.4.2-linux-x64/bin:$PATH
export LD_LIBRARY_PATH=/opt/oracle/instantclient:$LD_LIBRARY_PATH

npm install oracledb

The module is created in /home/nodejs/node_modules

Test the module

On Github there are several example scripts available for use with the node-oracledb module. See. node-oracledb examples

Download dbconfig.js and select1.js for a test of a db connection.
You can either change the dbconfig.js to match you db connection or set some environment variables.

export NODE_ORACLEDB_CONNECTIONSTRING=db01.domain.local:1521/fmwdb1.domain.local

Run the select1.js

This will perform a simple query on the departments table from the HR sample schema.

Some remarks

Set the environment variables permanent.

You can set the environment variables permanently for either the specific user or system wide.
Place them in the .bash_profile of the user that will run Node.js or create a .sh file in /etc/profile.d so the environment variables are set at logon for every user.

Install oracledb module global and set additional environment variable.

npm install -g oracledb

Set the environment variable NODE_PATH so Node.js knows where to find the modules.

export NODE_PATH=/opt/node-v4.4.2-linux-x64/lib/node_modules

Sources and references
node-oracledb on Github
Oracle Instant Client on OTN
Running node-oracledb – the Oracle Database Driver for Node.js – in the Pre Built VM for Database Development


About Post Author

Jaap Poot

Jaap Poot was a Principal Oracle Consultant at AMIS, an Oracle, Java, and SOA specialist based in The Netherlands. He worked as a consultant with the main focus on Fusion Middleware products. With wide knowledge in other areas such as Exalogic, ODA, Oracle VM, Linux, Database, provisioning, networking and several scripting languages.
0 %
0 %
0 %
0 %
0 %
0 %

Average Rating

5 Star
4 Star
3 Star
2 Star
1 Star

6 thoughts on “First setup of a connection from Node.js to an Oracle Database

  1. if I want to connect to remote oracle db then what would be the connection string on client side?

  2. It’s great to see clear steps with screenshots.

    Personally I find the Instant Client RPMs are easier to use on Linux unless you are going to install Instant Client in a location relative to some working directory, which wasn’t the case here since you installed in /opt/oracle. The node-oracledb installer search path checks the RPM location (see and will also use RPATH when linking, so you don’t need to set LD_LIBRARY_PATH at runtime.

    1. HI Christopher,

      You are right about that. rpm’s are easier when you want to install in the default location.
      I know some customers that want to install it iin their custom location. That’s where the zip files come in handy.

      Kind regards,

Comments are closed.

Next Post

Create an oracledb enabled Node.js application container

In my previous article, First setup of a connection from Node.js to an Oracle Database, I demonstrated how to make a connection to a remote database using Node.js and the node-oracledb module. I used a dedicated VM with Linux 7 installed and Oracle Instantclient provided the 12.1 client libraries. Now […]
%d bloggers like this: