Skip to main content

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

The source may not correspond directly with the resulting JavaScript. As a result, reported metadata (like vulnerability line-of-code and filename) references the compiled result, not the source.

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.

To use source maps, you must enable the Babel re-writer and rewrite caching in your YAML configuration:

agent:
  node:
    rewrite_cache:
      enable: true
      path: ./cache
    enable_babel: true

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.