oracle / node-oracledb

Oracle Database driver for Node.js maintained by Oracle Corp.
http://oracle.github.io/node-oracledb/
Other
2.24k stars 1.07k forks source link

Typescript OracleDB.getConnection is not a function #1514

Closed altoblond closed 1 year ago

altoblond commented 1 year ago
  1. What versions are you using?
process.platform: win32
process.version: v16.17.0
process.arch: x64
require('oracledb').versionString: 5.4.0
require('oracledb').oracleClientVersionString: 21.6.0.0.0
  1. Describe the problem

Hello, I have been working with node oracle db for some time in javascript but would like to switch to typescript. I did not manage to make it work so far.

Running this js script:

const OracleDB = require('oracledb');

const mypw = 'tutu';

async function run() {

    console.log(process.platform);
    console.log(process.version);
    console.log(process.arch);
    console.log(require('oracledb').versionString);
    console.log(require('oracledb').oracleClientVersionString);

    let connection;

    try {
        connection = await OracleDB.getConnection( {
            user          : "hr",
            password      : mypw,
            connectString : "localhost/XEPDB1"
        });
    } catch (err) {
        console.error(err);
    } finally {
        if (connection) {
            try {
                await connection.close();
            } catch (err) {
                console.error(err);
            }
        }
    }
}

run();

gives this output:

win32
v16.17.0
x64
5.4.0
21.6.0.0.0
Error: ORA-12541: TNS:no listener
    at async run (C:\maret\Spherea\GTSI\Software\POC\oracledb_typescript\src\app_js.js:20:22) {
  errorNum: 12541,
  offset: 0
}

Building and running the following typescript script:

import * as OracleDB from 'oracledb';

const mypw = 'tutu';

async function run() {

    console.log(process.platform);
    console.log(process.version);
    console.log(process.arch);
    console.log(require('oracledb').versionString);
    console.log(require('oracledb').oracleClientVersionString);

    let connection;

    try {
        connection = await OracleDB.getConnection( {
            user          : "hr",
            password      : mypw,
            connectString : "localhost/XEPDB1"
        });
    } catch (err) {
        console.error(err);
    } finally {
        if (connection) {
            try {
                await connection.close();
            } catch (err) {
                console.error(err);
            }
        }
    }
}

run();

gives this output:

> oracledb_typescript@1.0.0 start
> node dist/app.js

win32
v16.17.0
x64
5.4.0
21.6.0.0.0
TypeError: OracleDB.getConnection is not a function
    at run (C:\maret\Spherea\GTSI\Software\POC\oracledb_typescript\dist\app.js:34:37)
    at Object.<anonymous> (C:\maret\Spherea\GTSI\Software\POC\oracledb_typescript\dist\app.js:54:1)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

This is my package.json:

{
  "name": "oracledb_typescript",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "start": "node dist/app.js",
    "start_js": "node src/app_js.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/oracledb": "^5.2.3",
    "tslint": "^6.1.3",
    "typescript": "^4.4.4"
  },
  "dependencies": {
    "oracledb": "^5.4.0"
  }
}

And this is my tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "ES2021",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  },
  "lib": ["es2015"]
}

Could you please provide support?

altoblond commented 1 year ago

Replacing the import * with a require fixes the issue