oracle / node-oracledb

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

Error: Module not found: Can't resolve '@azure/app-configuration' #1691

Open Dongw1126 opened 3 weeks ago

Dongw1126 commented 3 weeks ago
  1. What versions are you using?
    • Node.js version
      Node.js: 20.16.0
      arch: x64
      platform: win32
  1. Is it an error or a hang or a crash?
    error

  2. What error(s) or behavior you are seeing?

Import trace for requested module:
./node_modules/oracledb/lib/configProviders/ sync ^\.\/.*$
./node_modules/oracledb/lib/oracledb.js
./node_modules/oracledb/index.js
./src/app/api/v1/test/route.js
 ⨯ ./node_modules/oracledb/lib/configProviders/azure.js:46:32
Module not found: Can't resolve '@azure/app-configuration'

https://nextjs.org/docs/messages/module-not-found
  1. Include a runnable Node.js script that shows the problem.

When I write the code below and call http://localhost:3000/api/v1/test,
I get "Module not found: Can't resolve '@azure/app-configuration'" error.

I haven't written any code related to azure and I'm getting an error even though I just added require('oracledb'). Please check this error.

I've also attached the minimal executable code as a zip file.

// app/api/v1/test/route.js

export const dynamic = 'force-dynamic'

const oracledb = require('oracledb');

export async function GET() {
    return Response.json('Hello', { status: 200 })
}

test.zip

sharadraju commented 3 weeks ago

Hi @Dongw1126 Are you using any package bundlers like esbuild or webpack in your application? If yes, please see https://github.com/oracle/node-oracledb/issues/1688

sharadraju commented 3 weeks ago

Next.js seems to use webpack, which tries to load the Azure modules, even if it is conditionally required. This is the same issue as https://github.com/oracle/node-oracledb/issues/1688

Dongw1126 commented 3 weeks ago

Hi @Dongw1126 Are you using any package bundlers like esbuild or webpack in your application? If yes, please see #1688

Thanks for the quick reply. I added "@azure/app-configuration": "^1.6.1" as a dependency and the error doesn't occur anymore. But wouldn't it be better to modify it so that it can run without having to install additional dependencies?

sharadraju commented 3 weeks ago

This is a webpack issue, where it does not ignore conditional requires. I will try to see if something can be done from our end to ensure that webpack can ignore them. See https://github.com/webpack/webpack/issues/8826 .

Dongw1126 commented 3 weeks ago

This is a webpack issue, where it does not ignore conditional requires. I will try to see if something can be done from our end to ensure that webpack can ignore them. See webpack/webpack#8826 .

Thank you.
If this is a duplicate issue, I think you can close it.

sharadraju commented 3 weeks ago

Closing this issue for now. We will try to find a solution on this.

sharadraju commented 3 weeks ago

@Dongw1126 Reopening this issue as I think I may found a temporary specific fix to work with frameworks like Next.js that use webpack. This is similar to the fix given in #1156. Here is how to apply the fix:

Unzip the attached configProviders.zip file to a new directory (D:\temp\configProviders)
Navigate to the application folder, where the node modules, including oracledb are installed (e.g., test/node_modules).
Navigate to the oracledb/lib folder.
Replace the configProviders directory in this directory with the new configProviders directory (i.e.,D:\temp\configProviders)
Remove the .next folder and rebuild the Next.js project again (npm run build).
Run npm run dev

configProviders.zip

Please try this fix and let me know.

I got the following output with http://localhost:3000/api/v1/test from your test.zip project after the fix: image

PS: Please note that is a temporary and very specific fix tailored for your test case and the final fix (if it is possible from our end) still needs to be worked out.

Dongw1126 commented 3 weeks ago

As you mentioned, modifying node_modules works fine. No more errors.
However, modifying node_modules should be added to my CICD pipeline. Or I could use patch-package. Anyway, both of the solutions you mentioned work fine.
Thank you.

sosoba commented 3 weeks ago

The ideal solution would be to move the Azure-enabled code into a separate package as a oracledb plugin and optionally include it in user project.

sharadraju commented 3 weeks ago

The ideal solution would be to move the Azure-enabled code into a separate package as a oracledb plugin and optionally include it in user project.

Thank you for the suggestion @sosoba. We will look into it.

niels-k-86 commented 2 weeks ago

You can set a custom Webpack config in NextJS. There you can mark packages as external. In your next.config.(m)js file, do this:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: config => {
    /**
     * These packages need to be added as external, else Oracle DB will try to load them due to a
     * Webpack bug.
     *
     * See these two issues for more information:
     * - https://github.com/oracle/node-oracledb/issues/1688
     * - https://github.com/oracle/node-oracledb/issues/1691
     **/
    config.externals.push(
      ...[
        "@azure/app-configuration",
        "@azure/identity",
        "@azure/keyvault-secrets",
        "oci-common",
        "oci-objectstorage",
        "oci-secrets",
      ],
    )

    return config
  },
}

export default nextConfig