Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
afsanefdaa opened this issue Jan 11, 2020 · 24 comments

Comments

@afsanefdaa
Copy link

afsanefdaa commented Jan 11, 2020

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

@afsanefdaa afsanefdaa changed the title [ error ] ./node_modules/@sentry/node/esm/integrations/console.js Module not found: Can't resolve 'console' in [ error ] ./node_modules/@sentry/node/esm/integrations/console.js Module not found: Can't resolve 'console' in - Sentry in Nextjs Jan 11, 2020
@afsanefdaa
Copy link
Author

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!

@gabrielperales
Copy link

@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?

@danielhusar
Copy link

danielhusar commented Mar 20, 2020

Add:

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

to the next.config.js

@komaljain
Copy link

Any solution to this?

@afsanefdaa
Copy link
Author

Any solution to this?

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

@stormfar
Copy link

stormfar commented Apr 7, 2020

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

@philkeys
Copy link

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

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

@anubnair
Copy link

anubnair commented May 5, 2020

i am still facing this issue:

@sentry/node^5.11.0

Anyone can help?

@stormfar
Copy link

stormfar commented May 5, 2020

@philkeys tried that already. No luck.

@Raudos
Copy link

Raudos commented May 13, 2020

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'; }

@williambrode
Copy link

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

@pixelass
Copy link

pixelass commented Jun 5, 2020

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

@kamilogorek
Copy link
Contributor

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.

@pixelass
Copy link

pixelass commented Jun 5, 2020

@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.

@guiaramos
Copy link

any solution?

@pixelass
Copy link

pixelass commented Sep 3, 2020

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.

@vpontis
Copy link

vpontis commented Sep 24, 2020

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...

@nynevi
Copy link

nynevi commented Oct 28, 2020

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
);

@nwaughachukwuma
Copy link

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'
  }

@Belline
Copy link

Belline commented Dec 14, 2020

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

@danohart
Copy link

danohart commented Dec 14, 2020

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.

  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.

@zackify
Copy link

zackify commented Dec 21, 2020

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

@CarlosFandango
Copy link

CarlosFandango commented Apr 18, 2021

Just in case anyone's not clear exactly how to add the fix to your next.config.js here's how I did it:

const isServer = (config) => config.name === 'server';

module.exports = {
  webpack: (config) => {
    if (!isServer(config)) {
      config.resolve.alias['@sentry/node'] = '@sentry/browser';
    }
    return config;
  }
  //... other config
}

and to @vpotis point below, you need to add the dependancy @sentry/browser to your package.json

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...

@bscaspar
Copy link

bscaspar commented May 7, 2021

For anyone using the latest update with @sentry/nextjs after running the new setup (which simplified things quite a bit), change all of your imports from import * as Sentry from '@sentry/node'; to import * as Sentry from '@sentry/nextjs';.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests