I already used Sentry in a C# project and was very pleased about it. So I also want to use it in a new NodeJS project with TypeScript. Sadly, the required sourcemaps doesn't work fine here. I first tried it in a plain TS project without success. Even in a plain raw JS project it doesn't do any source code mapping.
For the test project, I exactly followed the documentation on node/sourcemaps:
- https://docs.sentry.io/clients/node/config/
- https://docs.sentry.io/clients/node/sourcemaps/#webpack
- https://docs.sentry.io/clients/node/typescript/
The result is always the same, Sentry shows me the ugly code without using sourcemaps:
It seems that Sentry doesn't use the sourcemaps for unknown reasons cause the corresponding Release contains the required sourcemap:
I spend a lot of hours on this. Tried several things, including different paths/file names for the files pushed to sentry. I also used the Sentry cli tool directly, to make sure, that there is no issue with the webpack plugin. Nothing works, Sentry always shows me the raw source code and ignores the sourcemap.
The following files are from my second nodejs program using plain raw JavaScript. It's as minified as possible to work. I don't know whats wrong here cause I did exactly what the documentation requires.
What is the problem?
Sourcemaps were generated and uploaded. First I thought that the file names/paths doesn't match. This older question shows a similar issue, where the paths doesn't match since they were not relative using ~/
but in SentryPlugin
this is the default prefix. On the other side, dataCallback
make sure that all paths are relative to the project root, instead of having absolut paths like /home/user/project/dist/app.js
which would be the default behavior. I did a console.log
there to see that they're correctly relative.
Simple NodeJS test project using plain raw JavaScript
src/app.js (Main programm)
var Raven = require('raven')
const path = require('path')
let options = {
release: process.env.RELEASE,
dataCallback: function (data) {
var stacktrace = data.exception && data.exception[0].stacktrace;
if (stacktrace && stacktrace.frames) {
stacktrace.frames.forEach(function (frame) {
if (frame.filename.startsWith('/')) {
frame.filename = 'app:///' + path.basename(frame.filename);
}
});
}
return data;
}
}
Raven.config('http://<DnKey>@<SentryHost>:9000/3', options).install();
//Raven.captureException(new Error('abc'))
throw new Error('Hello123')
webpack.config.js
In this file, I changed the entry path from src/app.js
to ./src/app.js
. The Sentry docs use ./src/app.js
but this result in an error that the path cannot be resolved. It appears to be an mistake of the docs.
const path = require('path')
const fs = require('fs')
const SentryPlugin = require('@sentry/webpack-plugin')
module.exports = {
target: 'node',
devtool: 'source-map',
entry: {
"app": './src/app.js'
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
plugins: [
new SentryPlugin({
release: process.env.RELEASE,
configFile: 'sentry.properties',
include: './dist',
ignore: ['node_modules', 'webpack.config.js'],
})
]
};
sentry.properties
I added the configFile attribute since SentryPlugin
uses internally the CLI of Sentry. The CLI needs to know some information like url or auth-token from Sentry. This could be passed as environment variables, but I like the approach of using a config file, which is completely valid according to the docs. Token has project.write permission as required.
defaults.url=http://SentryHost:9000
defaults.org=sentry
defaults.project=js-test
auth.token=xxx
package.json
{
"name": "sentry-js-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "rm -rf dist/* && webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@sentry/cli": "^1.30.2",
"@sentry/webpack-plugin": "^1.3.3",
"raven": "^2.4.2",
"webpack": "^4.0.1"
},
"devDependencies": {
"webpack-cli": "^2.0.10"
}
}
Start the test programm
export RELEASE=3.0.20 && npm run build && node dist/app.js
This use webpack to minify src/app.js
and place the minified version in dist/app.js
. Furthermore, it creates a sourcemap called dist/app.js.map
.
On every run, I changed RELEASE
(e.g. increment to 3.0.21). That's important since Sentry assumes, that releases and source files (also sourcemaps) are a 1:1 relation. So if we upload our files to release with version 3.0.20 this is done on the first time. On the second time, the files keeps unchanged cause the cli sees that they're already existing. Using a new version make sure that our latest changed files are always uploaded and we don't mess with old ones from playing around.
I spend a lot of hours on this issue and have no Idea what I'm doing wrong. Summarized, I did every step from the tutorials of the documentation, with some small fixes on obvious errors.
Information about the environment
I'm using self hosted Sentry 8.22 in docker containers on Arch Linux. NodeJS is used in Version 9.7.1.