sentry-electron: TypeError: mod.require is not a function

Edited to add note from maintainers

This issue is very old so many of the solutions below are very outdated.

If you are getting this error, you are likely using a bundler which couldn’t handle the code that selects the correct code for each Electron process.

The docs now describe how you can ensure that the correct code is bundled for each process: https://docs.sentry.io/platforms/javascript/guides/electron/#bundler-configuration


Hello there,

Using electron 3 and sentry-electron 0.13.0, I can’t initialise Sentry and get this error when the app starts :

TypeError: mod.require is not a function
    at Object.dynamicRequire (webpack:///./node_modules/@sentry/utils/misc.js?:10:16)
    at Object.specificInit (webpack:///./node_modules/@sentry/electron/dist/dispatch.js?:129:41)
    at init (webpack:///./node_modules/@sentry/electron/dist/sdk.js?:52:16)
    at eval (webpack:///./src/sentry.js?:5:3)
    at Object../src/sentry.js (/Users/jtanay/Projects/misc/desktop-app/dist_electron/index.js:3730:1)
    at __webpack_require__ (/Users/jtanay/Projects/misc/desktop-app/dist_electron/index.js:20:30)
    at eval (webpack:///./src/background.js?:9:65)
    at Module../src/background.js (/Users/jtanay/Projects/misc/desktop-app/dist_electron/index.js:3719:1)
    at __webpack_require__ (/Users/jtanay/Projects/misc/desktop-app/dist_electron/index.js:20:30)
    at eval (webpack:///multi_./src/background.js?:1:18)

I just copy-pasted the docs from here : https://docs.sentry.io/error-reporting/quickstart/?platform=electron#configure-the-sdk and currently init Sentry only in my background process.

Did I miss something ?

Thanks for you help

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 20
  • Comments: 20 (6 by maintainers)

Most upvoted comments

I just released 0.15.0; could you guys try the following, if you have this issue instead of importing and using init like:

import * as Sentry from '@sentry/electron'
Sentry.init({...});

Use init on the main process with a deep import like:

import { init } from '@sentry/electron/dist/main'
import * as Sentry from '@sentry/electron'
init({...});

If this works I will write this into the docs.

We are now bundling both main and renderer processes with webpack. We use:

const { init } = (process.type === 'browser'
  ? require('@sentry/electron/dist/main')
  : require('@sentry/electron/dist/renderer'))

And then use the Webpack DefinePlugin:

    // main process
    plugins: [
      new webpack.DefinePlugin({
        'process.type': '"browser"'
      }),

   // renderer process
   new webpack.DefinePlugin({
     'process.type': '"renderer"'
   }),

This means that when bundling each process, Webpack replaces process.type with a constant and then removes the inaccessible code:

const { init } = ('browser' === 'browser'
  ? require('@sentry/electron/dist/main')
  : require('@sentry/electron/dist/renderer'))

// becomes >

const { init } = (true
  ? require('@sentry/electron/dist/main')
  : undefined)

@HazAT Could this be fixed? It’s been one year now and nearly any Electron trying out Sentry will waste hours on this and be left without any satisfactory solution. This is not up to par with Sentry overall quality.

To make this work in both main and renderer, I ended up monkey-patching the dispatch function:

require('@sentry/electron/dist/dispatch').specificInit =
    process.type === 'browser'
        ? require('@sentry/electron/dist/main').init
        : require('@sentry/electron/dist/renderer').init;

This is because we’re webpacking node_modules into our electron app (rather than shipping as node_modules) and we actually really want the normal require rather than the dynamic one electron is trying to use.

What if this project used a normal require, and for webpack builds you can always specify @sentry/electron as an external if you don’t want it to bundle it.

Getting the same error here, using Electron 3 We are using the Vue-CLI boilerplate, so you should be able to reproduce the error easily using https://github.com/nklayman/vue-cli-plugin-electron-builder

Not really sure were to start digging thought, first time using Sentry

@rlugge if it was possible to merge this into Sentry, I would have already submitted a PR 😁.

sentry-electron has to cater for many different development scenarios and it prioritises the most common which is bundled renderer and non-bundled main. For this most common scenario, there should be no special bundler configuration required and the renderer should not end up with all the unused Sentry main process code. dynamicRequire currently helps fulfil this and if it doesn’t for any reason, you should open a new issue about this.

Once the main process is bundled with Webpack, the node module object is often not available so the dynamicRequire call fails. Even if module.require() was available it’s likely the './main' code was never bundled.

Likely the simplest solution is to also expose InitMain() and InitRenderer() and leave it up to the developer to ensure the correct one is bundled and called, whether that be with help from webpack.DefinePlugin or some other means.