next.js 错误:NJS-045:无法为Node.js 18.14.0(win32 x64)加载node-oracledb二进制文件

von4xj4u  于 2023-03-29  发布在  Node.js
关注(0)|答案(1)|浏览(137)

我使用nextjs:13.1.6 oracle b:5.5.0
我收到以下错误:

Server Error
Error: NJS-045: cannot load a node-oracledb binary for Node.js 18.14.0 (win32 x64) 
  Looked for 
    C:\path\to\my\project\MyProjectName\.next\server\app\accounts\build\Release\oracledb-5.5.0-win32-x64.node, 
C:\path\to\my\project\MyProjectName\.next\server\app\accounts\build\Release\oracledb.node, 
C:\path\to\my\project\MyProjectName\.next\server\app\accounts\build\Debug\oracledb.node, 
C:\path\to\my\project\MyProjectName\.next\server\app\accounts\editUsernamePassword\node_modules\oracledb\build\Release\oracledb-5.5.0-win32-x64.node, 
C:\path\to\my\project\MyProjectName\.next\server\app\accounts\editUsernamePassword\node_modules\oracledb\build\Release\oracledb.node

  Node-oracledb installation instructions: https://oracle.github.io/node-oracledb/INSTALL.html

我的代码如下所示:\src\app\accounts\editUsernamePassword\page.js

\\first line of code
    import {oracleDB} from '@/library/database/oracle';

\src\library\database\oracle.js

const oracledb = require('oracledb');

//this is how to point to the Oracle Client without editing the PATH variable.
try {
  oracledb.initOracleClient({libDir: "C:\\pathToMyInstantClient\\oracle\\instantclient_21_9"});
} catch (err) {

  console.error('Unable to locate Oracle Instant Client.');
  console.error(err);
  process.exit(1);
} 
/// ... then, go on to use the oracledb object

第一个问题。从错误消息来看,它似乎无法找到oracledb*.node文件。这是正确的吗?起初,我以为它找不到Oracle Instant Client,但情况似乎并非如此。相反,问题是它找不到oracledb*.node文件。
第二,如果我的假设(在1中)是正确的,为什么它要查找相对于app/accounts页面的oracledb*.node?我想我可以将文件复制到那里,但这样我就需要将客户端复制到需要它的每个路径。
它不应该在这里寻找它吗:?我已经确认了oracledb*.node文件在这里:

C:\path\to\my\project\MyProjectName\node_modules\oracledb\build\Release

更让我困惑的是,这个导入并不总是失败。我有另一个文件:\src\library\oasis\abcHelper.js

\\first line of code
    import {oracleDB} from '@/library/database/oracle';
    
    //then, it goes on to successfully use the oracle library

我不知道为什么abcHelper.js能够加载库,但page.js在错误的位置查找库?
我确实在oracledb.js中看到了这条评论,他们似乎在讨论这个问题,但我不确定如何实现解决方案:

// For Webpack.  A Webpack copy plugin is still needed to copy 'node_modules/oracledb/build/' to the output directory
  // See https://github.com/oracle/node-oracledb/issues/1156

当我运行npm run dev时,我得到了这个警告,但我不确定这是否是根本原因。

wait  - compiling /accounts/editUsernamePassword/page (client and server)...
warn  - ./node_modules/oracledb/lib/oracledb.js
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Import trace for requested module:
./node_modules/oracledb/lib/oracledb.js
./node_modules/oracledb/index.js
./src/library/database/oracle.js
./src/app/accounts/editUsernamePassword/page.js

我当前的next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    appDir: true,
  },
}

module.exports = nextConfig
bvhaajcl

bvhaajcl1#

使用Webpack和node-oracledb 5.5时,需要在webpack.config.js中使用复制插件来复制node-oracledb二进制文件:

plugins: [
        new CopyPlugin([
            {
                // Copy the binary Oracle DB driver to dist.
                from: path.resolve(__dirname, 'node_modules/oracledb/build/Release'),
                to: 'oracledb',
            },
        ]),
        . . .
    ],

技巧请访问https://github.com/oracle/node-oracledb/issues/1156#issuecomment-598017250

相关问题