Search code examples
javascripthtmlnode.jsnode-oracledb

Node-oracledb in browser error "Module name has not been loaded for context yet"


I'm trying to load data from oracle database using node-oracledb and im getting this error :

Error: Module name "select1" has not been loaded yet for context: _. Use require([])

I did read the documentation here, but i couldnt figure out a solution .Why is it giving me such error and why thanks for the help.

select1.js :

   const oracledb = require('oracledb');
   const dbConfig = require('./dbconfig.js');
   const demoSetup = require('./demosetup.js');
   oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;
   async function run() {
   let connection;

   try {
   // Get a non-pooled connection

  connection = await oracledb.getConnection(dbConfig);

  await demoSetup.setupBf(connection);  // create the demo table

  var query = await connection.execute(
    // The statement to execute
    `SELECT * From pilote`,

    // The "bind value" 3 for the bind variable ":idbv"
    [],

    // Options argument.  Since the query only returns one
    // row, we can optimize memory usage by reducing the default
    // maxRows value.  For the complete list of other options see
    // the documentation.
    {
      maxRows: 100
      //, outFormat: oracledb.OUT_FORMAT_OBJECT  // query result format
      //, extendedMetaData: true                 // get extra metadata
      //, fetchArraySize: 100                    // internal buffer allocation size for tuning
    });
    //console.log(query.rows)

} catch (err) {
  console.error(err);
} finally {
  if (connection) {
    try {
      // Connections should always be released when not needed
      //await connection.close();
    } catch (err) {
      console.error(err);
    }
  }
}
return query;
}
module.exports.run =  run;

test.js :

(async() =>{
var result = require('./select1').run()
var rs;
rs = (await result).rows
if(rs !== undefined)
{
    document.getElementById('hdr').textContent = rs[0]
}
})()

page.html :

<body>
<h1 id="hdr">hello</h1>
<script src="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script>
<script  src="test.js"></script>
</body>

Solution

  • node-oracledb will not run in a browser because has a binary component that needs access to binary shared libraries. A standard Node.js application design has Node.js as a mid-tier application server that accepts networks calls e.g. REST, from the broswer and returns data to that browser. There are a few usage variants, e.g. as a standalone app using Electron.

    If it's all new to you, start with the example https://github.com/oracle/node-oracledb/blob/master/examples/webappawait.js and work up to https://jsao.io/2018/03/creating-a-rest-api-with-node-js-and-oracle-database/