Transpilers, compilers, source maps, and the Node.js agent
The Node.js agent supports applications written in languages that compile JavaScript, such TypeScript. Although the Node.js agent only instruments JavaScript, you can also use TypeScript if you configure the transpiler to compile your application into JavaScript.
Note
If your compiled files have source maps, the agent reports the original source file and line numbers in the stack traces it attaches to findings and attacks. Without source maps, it reports the compiled JavaScript.
Some languages, like TypeScript, require you to pre-compile your code before runtime. In these cases, the Node.js agent must point to the compiled entry point for your application.
To do this, set up the Node.js agent using the --import option:
scripts: {
"test": "...",
"start": "...",
"contrast": "node --import @contrast/agent /path/to/transpiled/entrypoint.js"
}Source maps
With a source map, you can see the corresponding line numbers between the TypeScript source and the transpiled JavaScript.
Source maps are on by default. The agent uses them to map locations in compiled JavaScript back to your source files. Make sure your compiler writes source maps and keeps the //# sourceMappingURL= comment in the compiled files. For TypeScript, add the --sourceMap flag to the compiler (tsc) or add "sourceMap": true to compilerOptions in tsconfig.json. To turn this behavior off, set agent.node.source_maps.enable to false.
When enabled, the agent looks for source maps (MAP files) in the same directory as the source that’s being loaded (for example, if the file /home/app/index.js is loaded, then Contrast looks for /home/app/index.js.map).
If you do not already have source maps then it needs to be recompiled using the necessary flags to produce source maps. This is different for every transpiler, so check the options for your transpiler. For example, for TypeScript, append the --source-map flag to the TypeScript compiler (tsc) or add the "sourceMap": true entry to the "compilerOptions" section in tsconfig.json.