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

Not able to import oracledb in Next.js 13 with typescript #1549

Closed mktdharmi closed 1 year ago

mktdharmi commented 1 year ago
  1. What versions are you using?

List of dependencies in project

"dependencies": { "@types/node": "18.15.11", "@types/react": "18.0.37", "@types/react-dom": "18.0.11", "autoprefixer": "10.4.14", "eslint": "8.38.0", "eslint-config-next": "13.3.0", "next": "13.3.1", "oracledb": "^5.5.0", "postcss": "8.4.22", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.1", "typescript": "5.0.4" }, "devDependencies": { "@types/oracledb": "^5.2.5" }

Oracle version Details Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production PL/SQL Release 11.2.0.3.0 - Production CORE 11.2.0.3.0 Production TNS for Linux: Version 11.2.0.3.0 - Production NLSRTL Version 11.2.0.3.0 - Production

  1. Describe the problem.

Following error is appearing in console on importing oracledb.

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Import trace for requested module: ./node_modules/.pnpm/oracledb@5.5.0/node_modules/oracledb/lib/oracledb.js ./node_modules/.pnpm/oracledb@5.5.0/node_modules/oracledb/index.js ./app/lib/connectDB.ts ./app/api/data/route.js error - node_modules.pnpm\oracledb@5.5.0\node_modules\oracledb\lib\oracledb.js (88:12) @ eval error - Error: NJS-045: cannot load a node-oracledb binary for Node.js 19.9.0 (win32 x64)

I am using following code for importing

const oracledb = require("oracledb");

I tried following code also. But it is also showing same problem. import oracledb from "oracledb";

Similar issue is reported on stackoverflow

https://stackoverflow.com/questions/75827237/error-njs-045-cannot-load-a-node-oracledb-binary-for-node-js-18-14-0-win32-x6

I tried to make an Express.js project with same oracledb version and typescript , there it is working fine. I am importing oracledb with following code import oracledb = require("oracledb"); But this type of import is not working in Next.js with typescript. It is showing following waring when hovering over the import statement.

*Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.ts(1202)**

cjbj commented 1 year ago

node-oracledb is a server side library, see https://github.com/vercel/next.js/issues/32286. You might want to use the node-oracledb 6.0 default 'Thin' mode in Next.js - though you would have to upgrade your DB from 11.2 to use this mode.

Your code would need to use Next.js Server-side Rendering since the front end won't have access to the Node.js libraries needed. If you use require('oracledb') at the top of your JS files you might see that the Node.js dns library can't be loaded. You have to do the load inside a routine that is invoked server-side.

You'll also have to figure out how to call async routines.

I was able to get a Next.js app to print SQL query output to the console, but my Next.js knowledge didn't let me plumb this into an HTML page in the time I spent on it.

cjbj commented 9 months ago

For future readers: regarding Next.js with node-oracledb, see a new blog post Using node-oracledb Thick mode in Next.js.