Code Whisperer
Code Whisperer

Reputation: 23652

ESLint - Prefer Export Default to Module.Exports

I was wondering if an ESLint rule exists, or how to make one, that does the following:

Allows exports only in the form export default foo and not in the form module.exports = foo

Is there a way to do this?

Upvotes: 7

Views: 10699

Answers (2)

Felix Oduro
Felix Oduro

Reputation: 51

module.exports is specific to Node. so add it to the env, like below

env: {
    browser: true,
    node: true,
    es2021: true,
},

Upvotes: 4

vitorbal
vitorbal

Reputation: 3031

There are no core rules that can do this, but the following plugin rule might be what you are looking for:

https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-commonjs.md

It will report any usage of CommonJS-style modules:

Invalid:

/*eslint no-commonjs: "error"*/
module.exports = foo;

Valid:

/*eslint no-commonjs: "error"*/
export default foo;

Upvotes: 8

Related Questions