Skip to main content

Common JS module system

When to test

  • Your package runs in Node.js
  • Your package is expected to be imported/required by projects
  • Your package uses a conditional export for the Common JS module system
    • Via the exports key in the package.json file

What to test

Test that your package works in projects that use the Common JS module system, the default module system in Node.js. This is especially important if your package is written and tested using the ES module system.

How to set up

Specify the module type in the package.json file:

test-projects/cjs-module-system/package.json
{
"type": "commonjs",
"scripts": {
"test": "jest"
}
}

Common problems to watch for

Require syntax

Make sure that your package can be required successfully and without using .default.

const yourPackage = require('your-package'); // Good
const yourPackage = require('your-package').default; // Bad