ESM (MJS)

Learn about running Sentry in an ESM application.

When running your application in ESM mode, you can't use require() to load modules. Instead, you have to use the --import command line options to load a module before the application starts:

Adjust the Node.js call for your application to use the --import parameter and point it at instrument.js, which contains your Sentry.init() code:

Copied
# Note: This is only available for Node v18.19.0 onwards.
node --import ./instrument.mjs app.mjs

We do not support ESM in Node versions before 18.19.0.

Most node applications today are either written in CommonJS (CJS), or compiled to CJS before running them. CommonJS uses require() to load modules. Our recommended installation method when using CommonJS is to require the instrument.js file at the top of your application. However, if your application is run in ESM mode, this will not work. In this case, you can follow the ESM docs.

Note that even if your application is written in ESM (using import), it may still be run in CJS. For example, almost all applications written in TypeScript are compiled to CJS before running them. In this case, you should follow the CommonJS instructions.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").