vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.53k stars 26.82k forks source link

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

Open PrinOrange opened 5 months ago

PrinOrange commented 5 months ago

Link to the code that reproduces this issue

https://github.com/PrinOrange/reproduction-app

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

KasperAndersson commented 5 months ago

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 commented 5 months ago

Experiencing the same issue as @KasperAndersson above. We have our own module that wraps Open Telemetry, and since 14.2, we've been observing issues that look related to packing @opentelemetry/instrumentation/build/src/platform/node/instrumentation.js.

Example:

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

Import trace for requested module:                                                                                                                                                                                 
./node_modules/@opentelemetry/instrumentation-pino/node_modules/@opentelemetry/instrumentation/build/esm/platform/node/instrumentation.js                                                                          
./node_modules/@opentelemetry/instrumentation-pino/node_modules/@opentelemetry/instrumentation/build/esm/platform/node/index.js                                                                                    
./node_modules/@opentelemetry/instrumentation-pino/node_modules/@opentelemetry/instrumentation/build/esm/platform/index.js                                                                                         
./node_modules/@opentelemetry/instrumentation-pino/node_modules/@opentelemetry/instrumentation/build/esm/index.js                                                                                                  
./node_modules/@opentelemetry/instrumentation-pino/build/src/instrumentation.js                                                                                                                                    
./node_modules/@opentelemetry/instrumentation-pino/build/src/index.js                                                                                                                                              
./node_modules/@opentelemetry/auto-instrumentations-node/build/src/utils.js                                                                                                                                        
./node_modules/@opentelemetry/auto-instrumentations-node/build/src/index.js                                                                                                                                        
./node_modules/@infinitaslearning/observability/dist/observability/configure-observability.js                                                                                                                      
./node_modules/@infinitaslearning/observability/dist/observability/index.js 

Is this an issue with how the open telemetry modules are distributed or with how they're bundled by next? It all worked tickety-boo until 14.2, and it did introduce changes to building.

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

andrem0 commented 3 months ago

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

KasperAndersson commented 3 months ago

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

s-nakagaki commented 2 months ago

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?