sentry-javascript: NextJS error on launch: Module not found: Can't resolve 'net'

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/nextjs

SDK Version

7.26.0

Framework Version

13.0.6

Link to Sentry event

No response

Steps to Reproduce

Update from ^7.21.1 to ^7.26.0 leads to an initialization error I did not have before.

I already have a webpack configuration which tell that NodeJS dependencies must not be resolved :

webpack: (config, { isServer }) => {
    if (!isServer) {
      config.resolve = {
        ...config.resolve,
        fallback: {
          // fixes proxy-agent dependencies
          net: false,
          dns: false,
          tls: false,
          assert: false,
          // fixes sentry dependencies
          process: false
        }
      };
    }
    return config;
  },
  sentry: {
    hideSourceMaps: true
  }

I cannot tell why this is happening.

Expected Result

Have no error on next command

Actual Result

error - ./node_modules/https-proxy-agent/dist/agent.js:15:0 Module not found: Can’t resolve ‘net’

Import trace for requested module: ./node_modules/https-proxy-agent/dist/index.js ./src/services/xx/xx.ts ./src/utils/xx/xx.ts ./src/utils/xx/xx.ts ./src/utils/xx/xx/Error.ts ./src/pages/_error.tsx

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

This usually happens when you forget to add ‘use server’ on a server action file. Happened to me couple of times 👍 . Not really an issue, but on nextjs side the error message can be better.

This usually happens when you forget to add ‘use server’ on a server action file. Happened to me couple of times 👍 . Not really an issue, but on nextjs side the error message can be better.

Thanks @dragos199993

This should be higher. For visibility:

This usually happens when you forget to add 'use server' on a server action file.

@dragos199993 Thanks, man. It’s been 20 minutes searching for what the issue is, and it turns out I forgot to add ‘use server’

On my end, the error was due to the import in client-side code of a server-side package ! Be careful with client / server-side imports mismatch and it might do the trick as it did for me.

I suggest looking at this thread where I go into a bit more detail: https://github.com/getsentry/sentry-javascript/issues/6339

TLDR: Plugins may return functions and you need to pass all args from the module.exports function to it OR you put the sentry plugin last like the docs suggest.

Finally works ! 😃 Thank you for the advice.

Can you please explain a little more what and why you modified ?

const withImages = require('next-images');
const { withSentryConfig } = require('@sentry/nextjs');
const { i18n } = require('./next-i18next.config');
const { redirects } = require('./next-redirects.config');

const nextConfig = {
  redirects,
  images: {
    disableStaticImages: true,
    domains: [
      ...
    ]
  },
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.resolve = {
        ...config.resolve,
        fallback: {
          // fixes proxy-agent dependencies
          net: false,
          dns: false,
          tls: false,
          assert: false,
          // fixes next-i18next dependencies
          path: false,
          fs: false,
          // fixes mapbox dependencies
          events: false,
          // fixes sentry dependencies
          process: false
        }
      };
    }
    config.module.exprContextCritical = false; // Workaround to suppress next-i18next warning, see https://github.com/isaachinman/next-i18next/issues/1545

    return config;
  },
  i18n,
  sentry: {
    hideSourceMaps: true
  },
  experimental: {
    largePageDataBytes: 192 * 1000
  }
};

const SentryWebpackPluginOptions = {
  // Additional config options for the Sentry Webpack plugin. Keep in mind that
  // the following options are set automatically, and overriding them is not
  // recommended:
  //   release, url, org, project, authToken, configFile, stripPrefix,
  //   urlPrefix, include, ignore

  silent: true // Suppresses all logs
  // For all available options, see:
  // https://github.com/getsentry/sentry-webpack-plugin#options.
};

module.exports = _phase => {
  const plugins = [withImages];
  return plugins.reduce((acc, plugin) => plugin(acc), {
    ...withSentryConfig(nextConfig, SentryWebpackPluginOptions)
  });
};

nextjs-i18n.config.js

const intervalPlural = require('i18next-intervalplural-postprocessor');

module.exports = {
  i18n: {
    locales: ['fr-FR', 'en-US', 'es-ES', 'de-DE', 'catchAll'],
    defaultLocale: 'catchAll'
  },
  defaultNS: 'translation',
  serializeConfig: false,
  fallbackLng: 'fr-FR',
  interpolation: {
    escapeValue: false
  },
  react: {
    transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p', 'u']
  },
  use: [intervalPlural],
  allowObjectInHTMLChildren: true
};

next-redirects.config.js

const locale = false;
const permanent = true;

module.exports = {
  async redirects() {
    return [
      { source: '/xxxx*', destination: '/fr-FR/xxxx*', permanent, locale },
...
    ];
  }
};