Sentry-javascript: [ error ] ./node_modules/@sentry/node/esm/integrations/console.js Module not found: Can't resolve 'console' in - Sentry in Nextjs

Created on 11 Jan 2020  ·  22Comments  ·  Source: getsentry/sentry-javascript

Package + Version

  • @sentry/browser ^5.11.0
  • @sentry/node^5.11.0
  • nodev12.7.0

Description

I have a nextjs application and I followed this tutorial provided by nextjs community. The problem is after building and running my app it gives this error:

[ error ] ./node_modules/@sentry/node/esm/integrations/console.js
Module not found: Can't resolve 'console' in '/.../node_modules/@sentry/node/esm/integrations'

I rebuild my app and removed my yarn lock and build folder but nothing happened! Although I have the directory in my node_modules!

The error is when I'm trying to import Sentry like this :

import * as Sentry from '@sentry/node';

Screenshot from 2020-01-11 12-56-20

Needs Triage

Most helpful comment

@5tormTrooper Not sure if this will help, but I forgot to add the () to the require statement:

const withSourceMaps = require('@zeit/next-source-maps')()

All 22 comments

I think the problem was the version of @zeit/next-source-maps which I changed to:
@zeit/next-source-maps": "0.0.4-canary.1.

Then I cleaned node_modules and removed yarn.lock and rebuild! It is working now!

@afsanefda I still have this issue after clean node_modules remove yarn.lock and having @zeit/next-source-maps": "0.0.4-canary.1 as dependency... Did you do any other thing that could have fixed the issue?

Add:

if (!options.isServer) {
  config.resolve.alias['@sentry/node'] = '@sentry/browser'
}

to the next.config.js

Any solution to this?

Any solution to this?

It's definitely because of the version inconsistency. please check versions as described above.

Folllowed the suggestion exactly and I'm still having this issue.

@5tormTrooper Not sure if this will help, but I forgot to add the () to the require statement:

const withSourceMaps = require('@zeit/next-source-maps')()

i am still facing this issue:

@sentry/node^5.11.0

Anyone can help?

@philkeys tried that already. No luck.

I faced same issue after upgrading Next.js from '9.1.6' to '9.3.5'. After some debugging i figured that '@sentry/node' somehow ended up in the client bundle. It seems that this line stopped working all of a sudden.

if (!isServer) { config.resolve.alias['@sentry/node'] = '@sentry/browser'; }

An we reopen this issue? I tried updating all packages and can't figure out how to resolve it.

Is there any solution for this issue? It seems to be such a generic feature that any library should support out of the box.

As you can tell, all referenced issues has been already somehow resolved by changing dev config. I'm not able to provide any specific fix if I'm not presented with a concrete problem and repro case.

@kamilogorek thx for the info. I only see one reference to an issue which seems to be unrelated. The OP suggested updating @zeit/next-source-maps(which we're not using)

I'll try to create a reproduction repo, though you'll have to provide a .env file with sentry tokens yourself.

any solution?

I was able to get it to work by closely replicating the example provided. I ran into this issue again when switching to SSG (next export) Since we don't need server-reports for SSG we simply removed the webpack alias (+ everything referencing @sentry/node) and it worked again. Now we only use @sentry/browser.

If you have this line:

if (!isServer) { config.resolve.alias['@sentry/node'] = '@sentry/browser'; }

Then you also need to install @sentry/browser. The error is a little misleading...

in my next.config.js, I was already doing the fs: "empty" method with also aliasing sentry/node with browser when !isServer however adding a console.log there that you can see your log is not printed on the console.

digging through the source code of the plugins I had (withSourceMaps, withSass, withOptimizedImages) and adding console.log in each of them, I realized my base config was not being sent to the outer plugins with this method of chaining (they seem to return the config themselves but something is still off):

module.exports = withBundleAnalyzer(
    withSourceMaps(
        withSass(
            withOptimizedImages(baseConfig)
        )
    )

so your fs: "empty" never gets respected.

the solution I found for this was to install next-compose-plugins package then change the export to

module.exports = withPlugins(
  [
    [withOptimizedImages],
    [withSass],
    [withSourceMaps],
    [withBundleAnalyzer],
  ],
  baseConfig
);

If you have this line:

if (!isServer) { config.resolve.alias['@sentry/node'] = '@sentry/browser'; }

Then you also need to install @sentry/browser. The error is a little misleading...

@vpontis, this didn't work for me. I'm using Sentry with Vue.js and have added this to vue.config.js

  chainWebpack: (config) => {
    config.resolve.alias['@sentry/node'] = '@sentry/browser'
  }

I want to add in case someone is still having issues.

If you are using next-source-maps, make sure the @zeit/next-source-maps package is set to 0.0.4-canary.1

I don't even have a need for a next.config.js, and I am not using sentry with next-source-maps so I'm not sure what the solution is for me.

if (process.env.NEXT_PUBLIC_SENTRY_DSN) { Sentry.init({ enabled: true, dsn: process.env.NEXT_PUBLIC_SENTRY_DSN, }); }

is what I put in my _app.js after I installed @sentry/node @sentry/browser @sentry/react and I still get this error.

I ran into this once I started using @zeit/next-source-maps": "0.0.4-canary.1 as well!

To fix it, I removed the plugin, and then added the important part of the code to the next config:

const { dev } = options

      if (!dev) {
        config.devtool = options.devtool || 'source-map'

        for (const plugin of config.plugins) {
          if (plugin.constructor.name === 'UglifyJsPlugin') {
            plugin.options.sourceMap = true
            break
          }
        }

        if (config.optimization && config.optimization.minimizer) {
          for (const plugin of config.optimization.minimizer) {
            if (plugin.constructor.name === 'TerserPlugin') {
              plugin.options.sourceMap = true
              break
            }
          }
        }
      }

It's not a problem with sentry, but with the next source map plugin

Was this page helpful?
0 / 5 - 0 ratings