next.js: Instrumentation hook is not called in `standalone` output

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: 8.5.0
    Relevant packages:
      next: 13.4.3-canary.1
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Standalone mode (output: “standalone”)

Link to the code that reproduces this issue

https://github.com/madebyherzblut/next-instrumentation-standalone

To Reproduce

  1. Create a new Next project using create-next-app
  2. Enable the instrumentationHook and standalone output in next.config.js
  3. Build the project using next build
  4. Run the server with .next/standalone/server.js

Describe the Bug

The instrumentation hook is executed with next dev, but not in standalone mode. Based on the example provided in https://github.com/vercel/next.js/issues/48262 I could trace the issue back to changes between next@13.3.5-canary.8 and next@13.3.5-canary.9. If the hook is called it should output “Instrumenting nodejs runtime” in the examples below:

$ pnpm add next@13.3.5-canary.8
[…]
$ NODE_ENV=production pnpm run build && node .next/standalone/server.js

[…]

Listening on port 3000 url: http://localhost:3000
Instrumenting nodejs runtime                                                # Correct
                                                                                                                                                                                     
$ rm -Rf .next node_modules
$ pnpm add next@13.3.5-canary.9
[…]
$ NODE_ENV=production pnpm run build && node .next/standalone/server.js

[…]

Listening on port 3000 url: http://localhost:3000                 # Missing output from hook
                                                                                                                                                                                      
$ pnpm run dev

[…]

event - compiled client and server successfully in 256 ms (326 modules)
Instrumenting nodejs runtime

$ pnpm add next@canary
Packages: +4 -3
++++---

dependencies:
- next 13.3.5-canary.9
+ next 13.4.3-canary.1

Progress: resolved 28, reused 20, downloaded 0, added 4, done
Done in 3.7s
$ NODE_ENV=production pnpm run build && node .next/standalone/server.js

[…]

Listening on port 3000 url: http://localhost:3000             # Missing output from hook
                                                                                         
$ pnpm run dev

> next-instrumentation-standalone@0.1.0 dev /Users/chris/Development/next-instrumentation-standalone
> next dev

- ready started server on 0.0.0.0:3000, url: http://localhost:3000
- warn You have enabled experimental feature (instrumentationHook) in next.config.js.
- warn Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.

- event compiled client and server successfully in 1770 ms (304 modules)
- wait compiling...
- wait compiling /instrumentation (client and server)...
- event compiled client and server successfully in 287 ms (319 modules)
Instrumenting nodejs runtime                                            # Correct in `dev` mode

Expected Behavior

The instrumentation hook should be executed in standalone outputs.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 35
  • Comments: 21

Commits related to this issue

Most upvoted comments

Hello everyone, I’d like to ask if you’re experiencing this issue only in standalone mode. I’m using version 14.0.3, and even with the normal next build && next start, I encounter the same problem. The register function is only called after receiving the request but works fine with next dev Is anyone else facing similar issues?

still same problem in Next.js 14.1.1-canary.52

Any update on this? I’m experiencing the exact same issue. The only workaround is to not use the standalone export mode.

vercel will not sell if standalone mode has implement properly…

@aNyMoRe0505 I’m experiencing the same issue. I chose to start a Queue worker using the instrumentation.ts file, but this doesn’t work after deployment. The register is not called at all even when I make a request.

Everything works smoothly locally

This bug is also present in 14.0.1. Works fine with next dev, but next build && node .next/standalone/server.js fails to call the register function until a request is received.

instrumentation seems to work in Next.js 14.2.0-canary.44 🎉 more in #63536

next.config.mjs

const nextConfig = {
  output: "standalone",
  compiler: {
    removeConsole: false,
  },
  experimental: {
    instrumentationHook: true
  }
};

export default nextConfig;

src/app/api/boot/boot.ts

const bootedServices = {
  service1: false,
};

export const bootHandler = async () => {
  if (!bootedServices.service1) {
    console.log("🐸 api/boot => SERVICE1");
    bootedServices.service1 = true;

    // your code here
  }
 return bootedServices;
};

src/app/api/boot/route.ts

export async function GET() {
  const bootedServices = await bootHandler();
  return NextResponse.json({ bootedServices });
}

export const dynamic = "force-dynamic";

src/instrumentation.ts

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    console.log('nodejs');
    setTimeout(async () => {
      await fetch('http://0.0.0.0:3000/api/boot', {cache:"no-store"});
    }, 3000);
  }
}

*tested in docker with file .npmrc includs node-linker=hoisted **code inspiration roneymaia -> code

Any update on this? I’m experiencing the exact same issue. The only workaround is to not use the standalone export mode.

i have the same problem

@jankaifer @sophiebits @feedthejim @huozhi instrumentation.js is missing in standalone mode even though #48615 was merged 😢

same problem as well

I believe I’m also experiencing this. I use standalone mode; register() function is called when using next dev, but when I deploy to Vercel, there’s no sign of it ever running. Using Next 13.4.19.

Only caveat to this is that I’m not sure that vercel deploys both respecting the standalone output? So perhaps my issue is something different.

Ran into the same problem with same behavior as @Aaqu reported. Using Next.js v13.5.4

Using workaround for now when using docker: Dockerfile

RUN chmod +x ./start.sh
CMD ["./start.sh"]

start.sh

#!/bin/bash
sleep 10 && curl "http://$(hostname):3000/" &> /dev/null &
node ".next/standalone/server.js"

I’ve created a separate issue for those experiencing similar issues without standalone mode enabled.

https://github.com/vercel/next.js/issues/59999

Has anyone found any workaround for this? I’ve already tried canary version, no chance. I’ve also found this article which is following a different approach, but it stopped working for me even in local environment