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

Uncaught TypeError: Cannot read property 'getAppPath' of undefined #254

Closed
6 of 7 tasks
chscott opened this issue Jul 28, 2020 · 13 comments
Closed
6 of 7 tasks

Uncaught TypeError: Cannot read property 'getAppPath' of undefined #254

chscott opened this issue Jul 28, 2020 · 13 comments

Comments

@chscott
Copy link

chscott commented Jul 28, 2020

Versions + Platform

  • SDK version - @sentry/electron@v1.5.1
  • Electron version - electron@v7.2.4
  • Platform - Windows

Description

I updated from 0.17.0 to 1.5.1, and I now see this error:

Uncaught TypeError: Cannot read property 'getAppPath' of undefined
    at eval (webpack-internal:///./app/node_modules/@sentry/electron/esm/main/normalize.js:8)
    at Module../app/node_modules/@sentry/electron/esm/main/normalize.js (renderer.js:1907)
    at __webpack_require__ (renderer.js:750)
    at fn (renderer.js:61)
    at eval (webpack-internal:///./app/node_modules/@sentry/electron/esm/main/backend.js:15)
    at Module../app/node_modules/@sentry/electron/esm/main/backend.js (renderer.js:1811)
    at __webpack_require__ (renderer.js:750)
    at fn (renderer.js:61)
    at eval (webpack-internal:///./app/node_modules/@sentry/electron/esm/main/client.js:8)
    at Module../app/node_modules/@sentry/electron/esm/main/client.js (renderer.js:1823)

I found a couple of issues (#92, #142) that reference this error, though they seem to be focused on different errors. I tried the deep import mentioned here, to no avail.

This was working with no issues previously, and the only change I made was to update to 1.5.1. I didn't see any breaking changes that suggest a problem in this area.

@timfish
Copy link
Collaborator

timfish commented Jul 28, 2020

Are you getting this error in the main or renderer process? You would expect this error if the code in normalize is run in the Electron renderer.

After 1.4.0 the package.json started referencing module and browser so Webpack should automatically pick the correct code for each Electron process without the deep import hack.

@chscott
Copy link
Author

chscott commented Jul 29, 2020

It's happening in the renderer process. I'm not sure what I'm doing wrong, as it was working just fine until the update. I did go back and confirm it worked in 1.3.2 and then broke in 1.4.0. Here's what I was doing the init in the renderer process:

import { init } from '@sentry/electron/dist/renderer'
import { SentryConfig, configureScope } from '../config'

let installed = false

if (!installed) {
  installed = true
  init(SentryConfig)

  configureScope()
}

I've now changed in the import to import { init } from '@sentry/electron', but I still have the same problem. I don't see anything out of step with what is shown in the documentation.

@timfish
Copy link
Collaborator

timfish commented Jul 29, 2020

For 1.4.0 and later, import { init } from '@sentry/electron' should work, but it relies on webpack being correctly configured.

Does your webpack config set target: 'electron-renderer'? Do you modify the defaults for resolve: { mainFields: ['browser', ...]}?

@chscott
Copy link
Author

chscott commented Jul 29, 2020

No, I have this configuration:

target: 'node',
  externals: ['electron'],

That configuration is a work-around for the issues described below.

sequelize/sequelize-typescript#717
webpack/webpack#7953
typeorm/typeorm#4210

@timfish
Copy link
Collaborator

timfish commented Jul 30, 2020

Yep, this will be the cause.
You're telling webpack that your renderer is a node process and this means it'll look at the main field in package.json which points at the code for the Electron main process.

In the renderer you're going to have to import the esm build manually. I've not actually tried this myself, but It'll be something like this:

import { init } from '@sentry/electron/esm/renderer'

init({})

@chscott
Copy link
Author

chscott commented Jul 30, 2020

I've tried this, but it doesn't work for me. From digging into webpack stats, it appears that I will need to go through the entire code base and conditionally require the main or renderer module everywhere I load anything related to @sentry/electron, otherwise I end up getting the main process modules in the renderer. It's pretty much a show-stopper since my webpack configuration has to be as described to allow more vital parts of the app to work.

I took another pass at it and have something that works, though it definitely feels sub-optimal.

Webpack config

export const main = {
  ...
  target: 'electron-main',
  plugins: [
    new webpack.DefinePlugin(
      Object.assign({}, replacements, {
        __PROCESS_KIND__: JSON.stringify('main'),
      })
    ),
  ],
})

export const renderer = {
  ...
  target: 'electron-renderer',
  // https://github.com/webpack/webpack/issues/7953#issuecomment-538353252
  resolve: {
    mainFields: ['module', 'main'],
  },
  plugins: [
    new webpack.DefinePlugin(
      Object.assign({}, replacements, {
        __PROCESS_KIND__: JSON.stringify('ui'),
      })
    ),
  ],
  ...

Code called by main or renderer

import { Severity } from '@sentry/types'
const Sentry =
  __PROCESS_KIND__ === 'main'
    ? require('@sentry/electron/dist/main')
    : require('@sentry/electron/dist/renderer')

I added a separate import for Severity because Typescript didn't like Sentry.Severity.

@b-zurg
Copy link

b-zurg commented Aug 6, 2020

None of the above approaches have worked correctly when including sentry in a preload script (i.e. webpack target electron-preload). Even loading the esm module directly was causing issues. Any other ideas?

Update: my issue was that I had a shared function that I used to init sentry and also configure scope etc. that would take init as a parameter. However I discovered that was not enough and that configureScope, and captureException also needed to be imported per main/renderer as above. My solution to this was to expose everything that was sensitive to the process as parameters that were then used as callbacks in my wrapper around all the sentry initialization.

@mehrdadmms
Copy link

@timfish
Hi
Can you give me a clear description on how to handle this issue ?
I don't completely understand what to do !
I tried importing configureScope, and captureException directly in preload script but I still get Uncaught TypeError: Cannot read property 'getAppPath' of undefined error.

I'm using electron+vue2

@timfish
Copy link
Collaborator

timfish commented Oct 11, 2021

Have you tried the 3.0.0-beta.1?

There has been a significant rewrite to help with issues like this.

@mehrdadmms
Copy link

How can I install version 3.0.0-beta.1 ? it's not released in npm.
also I tried to install using npm install --save git+https://github.com/getsentry/sentry-electron.git but I get dependency was not found error for @sentry/electron/main
@timfish

@timfish
Copy link
Collaborator

timfish commented Oct 11, 2021

It's a beta release so it's under the next tag.

I think you can install it via:

npm install @sentry/electron@next

You can't install from git because the git repository doesn't contain the build output.

@mehrdadmms
Copy link

@timfish
Thank you.
I was finally able to install and test it.
When I create errors in Vue (renderer) I receive the error in Sentry.
In background (main) I was able to capture the errors using captureExceptionToSentry.
But in preload and codes related to it when I just import @sentry/electron/preload and create errors I don't receive them in Sentry and when I want to import and use captureExceptionToSentry code crashes with two errors

  1. Cannot read property 'getPath' of undefined
  2. Communication with the Electron main process could not be established.

Also in migration issue #370 in preload script there's a short preload script link which leads to a 404 not found page.

@timfish
Copy link
Collaborator

timfish commented Oct 11, 2021

Link fixed.

I've opened a new issue for errors missing from the preload context: #372

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

No branches or pull requests

4 participants