commitlint icon indicating copy to clipboard operation
commitlint copied to clipboard

API modules export to `export.default` instead of `module.exports`

Open yaonyan opened this issue 5 years ago • 4 comments

The API packages cannot import modules with this code:

const format = require('@commitlint/format');
format({}); // `stderr`: TypeError: format is not a function

While import from default works:

const format = require('@commitlint/format').default;
format({})

Current Behavior

After I do npm install --save @commitlint/format, then look into node_modules/index.js:

Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(format_1).default; } });

That means the complied typescript code export itself to module.exports.default, not module.exports

Affected packages

All the API Packages with newest version

Possible Solution

We can change the documentation or the codebase

Steps to Reproduce (for bugs)

Test it on runkit. At first, it was so puzzling that I almost gave up using it ☺, please fix that, THANKS!

Your Environment

Executable Version
node --version v14.15.1

yaonyan avatar Jan 22 '21 05:01 yaonyan

Hey @beetcb , is #2405 related or maybe the same?

escapedcat avatar Jan 22 '21 05:01 escapedcat

As as far as I know, format is a separate module and does not need to set commitlint.config.js. Yes, both #2405 and this have the same issue: something about CommonJS and ECMAScript module

yaonyan avatar Jan 22 '21 07:01 yaonyan

Anyway, the output should be something like this, can ts complier fix that (i know nothing about typescript)?

Object.defineProperty(module, 'exports', {
  enumerable: true,
  get: function () { return __importDefault(format_1).default; }
});

Or for simplicity, just change the documentation, add .default

yaonyan avatar Jan 22 '21 07:01 yaonyan

Happy for any PR

escapedcat avatar Jan 22 '21 08:01 escapedcat