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

uncaughtException: rollbar_1.default is not a constructor #1089

Open
surisoko opened this issue Jan 11, 2023 · 5 comments
Open

uncaughtException: rollbar_1.default is not a constructor #1089

surisoko opened this issue Jan 11, 2023 · 5 comments

Comments

@surisoko
Copy link

This error is thrown when I deploy my nodejs project

TypeError: rollbar_1.default is not a constructor
    at rollbarInit (/dist/rollbar.js:9:12)
    at Object.<anonymous> (/dist/app.js:28:39)
    at Module._compile (node:internal/modules/cjs/loader:1155:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Module.require (node:internal/modules/cjs/loader:1057:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/dist/server.js:8:15)
    at Module._compile (node:internal/modules/cjs/loader:1155:14)

And my code:
rollbar.ts

const Rollbar = require('rollbar')

export const rollbar = new Rollbar({
  accessToken: MY_ROLLBAR_KEY,
  captureUncaught: true,
  captureUnhandledRejections: true,
})

app.ts

import { rollbar } from './rollbar'

app.use(rollbar.errorHandler())

nodejs: 16
express: 4.18.2
rollbar: 2.26.1

@waltjones
Copy link
Contributor

@surisoko Thank you for the report.

const Rollbar = require('rollbar')

should work when setting "module": "commonjs" in your tsconfig compilerOptions.

Node 16 supports ES modules, and allows setting "module": "ES2022" or other ES versions. In this case, the correct syntax is:

import Rollbar from 'rollbar';

In your tsconfig compilerOptions, you should also set "esModuleInterop": true.

This is covered in the doc here: https://docs.rollbar.com/docs/importing-or-requiring-rollbar

@surisoko
Copy link
Author

@surisoko Thank you for the report.

const Rollbar = require('rollbar')

should work when setting "module": "commonjs" in your tsconfig compilerOptions.

Node 16 supports ES modules, and allows setting "module": "ES2022" or other ES versions. In this case, the correct syntax is:

import Rollbar from 'rollbar';

In your tsconfig compilerOptions, you should also set "esModuleInterop": true.

This is covered in the doc here: https://docs.rollbar.com/docs/importing-or-requiring-rollbar

Hi @waltjones
I've tried import Rollbar from 'rollbar' with no luck, same error
My tsconfig.json:

"compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "target": "ES2018",
    ...
}

@waltjones
Copy link
Contributor

@surisoko It looks like you might be setting "type": "module" in package.json?

@surisoko
Copy link
Author

There is no such configuration in my package.json

@surisoko
Copy link
Author

The funny thing is that it doesn't work if I import rollbar from app.ts, but does work from other subfolders

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

2 participants