next.js: [NEXT-1173] use node module inside instrumentation hook cause module not found error

Verify canary release

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

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #22 SMP Tue Jan 10 18:39:00 UTC 2023
    Binaries:
      Node: 16.17.0
      npm: 8.15.0
      Yarn: 1.22.19
      pnpm: 7.1.0
    Relevant packages:
      next: 13.4.2-canary.4
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.4

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

No response

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/awesome-roman-jkuyps?file=%2Fpages%2Findex.tsx%3A3%2C10-3%2C14

To Reproduce

run yarn dev in codesandbox

Describe the Bug

use node module like path (except process, because it has been defined in webpack.resolve.fallback) inside instrumentation.ts file will cause module not found error

also, it can be temporarily solved by adding browser field in package.json


"browser": {
    "path": false
  }

image

Expected Behavior

no error since it’s a server startup config, it’s highly likely to use nodejs module(s)

Which browser are you using? (if relevant)

Chrome 100.0.4878.0

How are you deploying your application? (if relevant)

next start

NEXT-1173

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 8
  • Comments: 24 (2 by maintainers)

Most upvoted comments

Hi, was trying this feature out and faced the same problem at first. Then read again the documentation and realized that I needed to use import inside the function. The other issue is that you need to check that your are running your code in the correct runtime. Seen your codesandbox and this should fix it:

instrumentation.ts:

export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    const { join } = await import("path");
    const { cwd, exit, platform } = await import("process");
    console.log(join("a", "b"));
    console.log(cwd());
  }
}

Hope this helps

Ran into this issue with 13.5.4, originally, I had

if (process.env.NEXT_RUNTIME !== "nodejs") return;
// imports and do something

but apparently this does not work, instead it must be

if (process.env.NEXT_RUNTIME === "nodejs") {
  // imports and do something
}

for some reason…found out that you must put those imports in:

export async function register() {
    if (process.env.NEXT_RUNTIME === "nodejs") {
        console.log("instrumentation register");
        //await import packages...
    }
}

not only using await, the trick seems to be “if (process.env.NEXT_RUNTIME === “nodejs”) {…}”, you have to do things inside this “IF”, I don’t know why, but it worked well, otherwise didn’t.

I’m running into this in dependencies.

If I attempt to await import('winston'), it errors on os being required by the @colors/colors dependency in winston.

Forked from @justLuiz’s example:

Just importing winston

My problem with this feature is I wanted something to run on server start and not when the first browser window opens. It seems that its not the case even though they are marketing it as start with the server: If you export a function named register from this file, we will call that function whenever a new Next.js server instance is bootstrapped.

I have the same problem, This looks like a bug.

Hi, was trying this feature out and faced the same problem at first. Then read again the documentation and realized that I needed to use import inside the function. The other issue is that you need to check that your are running your code in the correct runtime. Seen your codesandbox and this should fix it:

instrumentation.ts:

export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    const { join } = await import("path");
    const { cwd, exit, platform } = await import("process");
    console.log(join("a", "b"));
    console.log(cwd());
  }
}

Hope this helps

it works,thanks! maybe the document should describe it more clear

here is what i did -

i wanted to execute functions in my next.js application on every startup - i tried using instrumentation.ts but it was always throwing error about fs and other node modules, so what i did is make a GET api route (Next 14) and use fetch from instrumentation.ts. It worked like charm 🎉

export function register() {
  fetch('http://localhost:3000/api/init')
}

i have hard coded the URL, but you can use process.env.VERCEL_URL

I’m also hitting a similar issue:

Error: Module not found: Can't resolve 'net'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/.pnpm/postgres@3.3.5/node_modules/postgres/src/index.js
./src/app/api/db/index.ts
./src/instrumentation.ts

For my case, I’m running my DB migrations on server start, and I import the migration function from my DB lib. From the above solution, it looks like I may need to pull in my DB connection and migration logic directly into register, but this is not ideal.

My problem with this feature is I wanted something to run on server start and not when the first browser window opens. It seems that its not the case even though they are marketing it as start with the server: If you export a function named register from this file, we will call that function whenever a new Next.js server instance is bootstrapped.

Also running into this issue when attempting to bootstrap the https://github.com/microsoft/ApplicationInsights-node.js package.

Inlining the logic into the register function doesn’t work as the Node runtime dependencies are only used by the applicationinsights package. Using the workaround with the browser config is a non-starter as that would require explicitly declaring [node package name]: false for over a dozen packages, when those packages should be resolvable in the nodejs runtime without issue.