Skip to content

Next.js Instrumentation can not import and resolve the node-bindings module. #64471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
PrinOrange opened this issue Apr 14, 2024 · 10 comments
Open
Labels
bug Issue was opened via the bug report template. Instrumentation Related to Next.js Instrumentation.

Comments

@PrinOrange
Copy link

PrinOrange commented Apr 14, 2024

Link to the code that reproduces this issue

https://github.com/bugs-report/nextjs-issue-64471

To Reproduce

First, install a node-binding package, like the @napi-rs/tar, which provides the rust bindings and .node bytecode module.

npm i @napi-rs/tar

And then enable the next.js instrumentation with the official document

In next.config.js

/** @type {import("next").NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  experimental: {
    instrumentationHook: true,
  },
};

module.exports = nextConfig;

In instrumentation.ts, import the node-binding module

import tar from '@napi-rs/tar';

export function register() {
  console.log(tar.Entry)
}

Then run project in dev, it crashed

 ✓ Starting...
 ○ Compiling /instrumentation ...
 ⨯ ./node_modules/@napi-rs/tar-win32-x64-msvc/tar.win32-x64-msvc.node
**Module parse failed: Unexpected character '�' (1:2)**
**You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders**
(Source code omitted for this binary file)

Import trace for requested module:
./node_modules/@napi-rs/tar-win32-x64-msvc/tar.win32-x64-msvc.node
./node_modules/@napi-rs/tar/index.js
Error: An error occurred while loading instrumentation hook: Cannot find module 'E:\frontend-projects\reproduction-app\.next\server\instrumentation'

Current vs. Expected behavior

It should import the node-bindings module in instrumentation.ts

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 36670
  Available CPU cores: 8
Binaries:
  Node: 20.10.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.1-canary.4 // Latest available version is detected (14.2.1-canary.4).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@PrinOrange PrinOrange added the bug Issue was opened via the bug report template. label Apr 14, 2024
@balazsorban44 balazsorban44 added the Instrumentation Related to Next.js Instrumentation. label Apr 19, 2024
@KasperAndersson
Copy link

I'm having the same issue, with Application Insights. This issue was introduced with Next 14.2.X.

Dependencies

    "@microsoft/applicationinsights-react-js": "^17.1.2",
    "@microsoft/applicationinsights-web": "^3.1.2",
    "applicationinsights": "^3.0.0",
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.2.2
  eslint-config-next: 14.2.2
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.0.4
Next.js Config:
  output: standalone

Output on yarn dev

  - Local:        http://localhost:3000
  - Environments: .env.local, .env.development
  - Experiments (use with caution):
    · instrumentationHook

 ✓ Starting...
next-type-safe-routes: types written to @types/next-type-safe-routes
next-type-safe-routes: types written to @types/next-type-safe-routes
next-type-safe-routes: types written to @types/next-type-safe-routes
 ○ Compiling /instrumentation ...
 ⚠ ./node_modules/@azure/monitor-opentelemetry/dist/index.js
Module not found: Can't resolve '@azure/functions-core' in '[PROJECT_FOLDER]/node_modules/@azure/monitor-opentelemetry/dist'

Import trace for requested module:
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/dist/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js
./node_modules/diagnostic-channel-publishers/dist/src/index.js
./node_modules/applicationinsights/out/src/logs/diagnostic-channel/initialization.js
./node_modules/applicationinsights/out/src/logs/autoCollectLogs.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
Module not found: Can't resolve '@opentelemetry/exporter-jaeger' in '[PROJECT_FOLDER]/node_modules/@opentelemetry/sdk-node/build/src'

Import trace for requested module:
./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
./node_modules/@opentelemetry/sdk-node/build/src/sdk.js
./node_modules/@opentelemetry/sdk-node/build/src/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js
 ✓ Ready in 5.4s
 ⚠ ./node_modules/@azure/monitor-opentelemetry/dist/index.js
Module not found: Can't resolve '@azure/functions-core' in '[PROJECT_FOLDER]/node_modules/@azure/monitor-opentelemetry/dist'

Import trace for requested module:
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/dist/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js
./node_modules/diagnostic-channel-publishers/dist/src/index.js
./node_modules/applicationinsights/out/src/logs/diagnostic-channel/initialization.js
./node_modules/applicationinsights/out/src/logs/autoCollectLogs.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
Module not found: Can't resolve '@opentelemetry/exporter-jaeger' in '[PROJECT_FOLDER]/node_modules/@opentelemetry/sdk-node/build/src'

Import trace for requested module:
./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
./node_modules/@opentelemetry/sdk-node/build/src/sdk.js
./node_modules/@opentelemetry/sdk-node/build/src/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js
 ⚠ ./node_modules/@azure/monitor-opentelemetry/dist/index.js
Module not found: Can't resolve '@azure/functions-core' in '[PROJECT_FOLDER]/node_modules/@azure/monitor-opentelemetry/dist'

Import trace for requested module:
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/dist/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js
./node_modules/diagnostic-channel-publishers/dist/src/index.js
./node_modules/applicationinsights/out/src/logs/diagnostic-channel/initialization.js
./node_modules/applicationinsights/out/src/logs/autoCollectLogs.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
Module not found: Can't resolve '@opentelemetry/exporter-jaeger' in '[PROJECT_FOLDER]/node_modules/@opentelemetry/sdk-node/build/src'

Import trace for requested module:
./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
./node_modules/@opentelemetry/sdk-node/build/src/sdk.js
./node_modules/@opentelemetry/sdk-node/build/src/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js
 ⚠ ./node_modules/@azure/monitor-opentelemetry/dist/index.js
Module not found: Can't resolve '@azure/functions-core' in '[PROJECT_FOLDER]/node_modules/@azure/monitor-opentelemetry/dist'

Import trace for requested module:
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/dist/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js
./node_modules/diagnostic-channel-publishers/dist/src/index.js
./node_modules/applicationinsights/out/src/logs/diagnostic-channel/initialization.js
./node_modules/applicationinsights/out/src/logs/autoCollectLogs.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
Module not found: Can't resolve '@opentelemetry/exporter-jaeger' in '[PROJECT_FOLDER]/node_modules/@opentelemetry/sdk-node/build/src'

Import trace for requested module:
./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
./node_modules/@opentelemetry/sdk-node/build/src/sdk.js
./node_modules/@opentelemetry/sdk-node/build/src/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

@BorePlusPlus

This comment has been minimized.

@andrem0
Copy link

andrem0 commented Jun 11, 2024

@KasperAndersson did you find any solution or work around? We are experiencing the same issue.

@KasperAndersson
Copy link

Hi @andrem0
No, I didn't. I'm also following microsoft/ApplicationInsights-node.js#1102 - and we're working on migrating to Sentry.io instead of AI.

@s-nakagaki
Copy link

I got a simular error.
instrumentation.ts does not seem to work with @azure/monitor-opentelemetry which I'm trying.

The error is below.

Error [TypeError]: Native module not found: ./edge-runtime-webpack.js

Is there a way to call once at server-side without instrumentation.ts?

@DmytroGolovin
Copy link

I had simmilar issues with azure app insights and I did a work around like this:
image

@kristian240
Copy link

@KasperAndersson I had similar issue, check if your OTEL dependecies have the same versions.

@john-james-gh
Copy link

john-james-gh commented Jan 13, 2025

This worked for me in case you wanna try it.

Next.js version: 14.2.23
Application Insights version: 3.4.0

// next.config.js

module.exports = {
    experimental: {
        instrumentationHook: true,
    },
    webpack: (config, { isServer }) => {
        if (isServer) {
            // Ensure the externals array exists
            config.externals = config.externals || [];

            // Add the specific modules you want to externalize
            config.externals.push(
                "@azure/functions-core",
                "@opentelemetry/exporter-jaeger"
            );
        }

        return config;
    },
};
// instrumentation.ts

export function register() {
    if (process.env.NEXT_RUNTIME === "nodejs") {
        import("applicationinsights")
            .then((ai) => {
                ai.setup(
                    "<APPLICATIONINSIGHTS_CONNECTION_STRING>"
                ).start();

                ai.defaultClient.trackEvent({ name: "AI Started" });
            })
            .catch((err) => {
                console.error("Failed to load Application Insights", err);
            });
    }
};

Image

@jonlambert
Copy link

jonlambert commented Jan 23, 2025

As a general note, we're having a similar issue in Next.js 13.5.8 where requiring files in instrumentation.ts results in a swallowed error. If I wrap the minified call to import in a try/catch, I get an error warning me that an @opentelemetry/<any module> module couldn't be resolved. I'm not sure this is restricted to just packages with native bindings 😞

@ricardasjak
Copy link

ricardasjak commented May 15, 2025

my solution to this problem:

add
import * as ApplicationInsightsNodejs from 'applicationinsights';
or similar reference in your actual web app server side code. If you don't add the reference, nextjs bundler omits it, and instrumentation.js fails to run.

I also had issues trying to use ts file, so I simply went with instrumentation.js version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Instrumentation Related to Next.js Instrumentation.
Projects
None yet
Development

No branches or pull requests