Ole
Ole

Reputation: 47088

Commitlint error message: ReferenceError: module is not defined in ES module scope

I've created a starter project for open source projects that includes features like commitlint, release-it, etc.

https://github.com/fireflysemantics/fs-github-flow-docs-starter

If we clone it down like this:

git clone https://github.com/fireflysemantics/fs-github-flow-docs-starter

And add "type": "module" to package.json and try to do a commit like this:

git add package.json 
git commit -m "fail"

Instead of the normal commitlint message that looks something like this:

⧗   input: fail
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings

We get:

ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/commitlint.config.js:1:1
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadJs (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/loaders.js:23:17)
    at async #loadConfiguration (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/Explorer.js:116:36)
    at async #loadConfigFileWithImports (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/Explorer.js:87:31)
    at async #readConfiguration (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/Explorer.js:84:51)
    at async search (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/Explorer.js:50:40)
    at async Explorer.search (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/Explorer.js:78:20)
    at async loadConfig (file:///Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/@commitlint/load/lib/utils/load-config.js:53:19) {
  filepath: '/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/commitlint.config.js'
}

And the reason for this is that node wants the format to be in ES module format if we add the "type": "module" property to package.json, however if this is not present the configuration that is shipped with the project works.

If we add "type": "module" then this configuration will work.

const Configuration = { extends: ['@commitlint/config-conventional'] };
export default Configuration;

However if this configuration is used without "type": "module" being set then this error is thrown:

SyntaxError: Unexpected token 'export'
    at wrapSafe (node:internal/modules/cjs/loader:1281:20)
    at Module._compile (node:internal/modules/cjs/loader:1321:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at Module.require (node:internal/modules/cjs/loader:1233:19)
    at module.exports (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/import-fresh/index.js:32:59)
    at loadJsSync (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/loaders.js:17:12)
    at loadJs (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/loaders.js:27:43)
    at async #loadConfiguration (/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/node_modules/cosmiconfig/dist/Explorer.js:116:36) {
  filepath: '/Users/oleersoy/Github/RM1/fs-github-flow-docs-starter/commitlint.config.js'
}

Ideally there would be a configuration that can be loaded that is "Indifferent" to whether "type": "module" is specified or not, that way developers who extend The Github Flow Starter Project can do so without getting errors / having to change the commitlint configuration.

Thoughts?

Upvotes: 1

Views: 746

Answers (1)

Ole
Ole

Reputation: 47088

OK we can have the file as an .mjs file, and use the ES Module format configuration and then it will work regardless of whether "type": "module" is set in package.json.

I've updated the starter project configuration in case anyone wants something to play with.

Upvotes: 0

Related Questions