pino-sentry icon indicating copy to clipboard operation
pino-sentry copied to clipboard

"Can't resolve 'child_process'" error when using pino-sentry with Next.js

Open grazianodev opened this issue 3 years ago • 8 comments

Hello,

I've installed pino-sentry in a Next.js project, however even by just importing from the package like this:

import pino from "pino"
import { createWriteStream } from "pino-sentry"
    
module.exports = pino({})

I get the following error:

./node_modules/@sentry/node/esm/integrations/context.js:2:0
Module not found: Can't resolve 'child_process'
    
Import trace for requested module:
./node_modules/@sentry/node/esm/sdk.js
./node_modules/@sentry/node/esm/index.js
./node_modules/pino-sentry/dist/transport.js
./node_modules/pino-sentry/dist/index.js
./logger/index.js
./helpers/fetch.js
./hooks/useViewer.js
./components/AppHeader/index.js
./pages/_app.js
    
https://nextjs.org/docs/messages/module-not-found

I would really like to try and use pino-sentry so any help would be appreciated! These are my package versions in case it matters:

"@sentry/nextjs": "^7.14.2"
"next": "^12.2.3",
"pino": "^8.6.1",
"pino-sentry": "^0.13.0",
"react": "^18.1.0",
"react-dom": "^18.1.0"

grazianodev avatar Oct 10 '22 06:10 grazianodev

  1. did you read the url your error message included?
  2. make standalone gist for reproducer

glensc avatar Oct 10 '22 10:10 glensc

Yes, I read the url. I guess you mean that the error is due to the Sentry package, however I only get that error when using pino-sentry, so I'm not sure... sorry if I made a mistake.

Anyway, I did create a reproduction. It's a basic create-next-app, to which I've only added the packages and logger code that I posted in my first message.

grazianodev avatar Oct 10 '22 16:10 grazianodev

I'm guessing you're trying to load pino to client side code:

and hence the error.

I'm not familiar with next.js, so it might take some time to for me to process it through

however, the problem could be also the way next.js does bundling. pino has some docs about it:

  • https://github.com/pinojs/pino/blob/v8.6.1/docs/bundling.md

glensc avatar Oct 11 '22 09:10 glensc

you forgot to add instructions. and what's the reproduce steps there? yarn && yarn dev?

glensc avatar Oct 11 '22 09:10 glensc

I'm guessing you're trying to load pino to client side code

I do successfully use pino on the client side already and I do not get that error. The error only shows up as soon as I import createWriteStream from pino-sentry.

As for the repro, I use npm and do npm install and then npm run dev, which I think is the equivalent yarn && yarn dev. Once you launch the app you immediately get the error, but then if you go into logger.js (in the root folder) and comment out import { createWriteStream } from "pino-sentry", the error goes away, the page loads fine and in the console you should read This is a test log, which is a log I added in the homepage with pino's logger.info(which, incidentally, should demonstrate that pino works on the client).

Thanks for your help!

grazianodev avatar Oct 11 '22 12:10 grazianodev

no error here

➜ yarn && yarn dev
yarn install v1.22.19
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] 🔍  Resolving packages...
success Already up-to-date.
✨  Done in 0.34s.
yarn run v1.22.19
$ next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
event - compiled client and server successfully in 654 ms (173 modules)

glensc avatar Oct 11 '22 13:10 glensc

Hello @glensc

I am currently facing the same issue. I have just added into my project pino-sentry and faced the same issue.

Maybe is a node.js related issue?

My versions are

    "@sentry/nextjs": "7.11.1",
    "pino-sentry": "0.13.0",
     "next": "12.2.5",
node version: v16.15.1

I will try to reproduce the repo or use the repo provided by @grazianodev

thanks!

EDIT: I have cloned the reproduction repo and I got the same issue

image

My steps:

  1. yarn, or npm i
  2. yarn dev or npm run dev
  3. go to localhost:3000
  4. 💥

Thanks!

EDIT 2: I wonder if this could be a dependencies version issue?

in transport.js you are referencing child_process

And I have spotted this issue from the original Sentry integration https://github.com/getsentry/sentry-javascript/issues/5787

sauldeleon avatar Oct 20 '22 14:10 sauldeleon

Same issue here! Using NextJS T3 Stack.

namdao2000 avatar Aug 23 '23 21:08 namdao2000