eslint-plugin-optimal-modules
An ESLint plugin to enforce optimal JavaScript module design.
Installation
To install eslint-plugin-optimal-modules
with npm, run:
npm install eslint-plugin-optimal-modules --save-dev
To use the recommended config, add the following ESLint config:
{
"extends": ["plugin:optimal-modules/recommended"]
}
Alternatively, manually configure the plugin and the desired rules:
{
"plugins": ["optimal-modules"],
"rules": {
"optimal-modules/no-named-exports": "error"
}
}
To allow named exports in Storybook story modules that have a Component Story Format (CSF), add this extra ESLint config:
{
"overrides": [
{
"files": ["*.stories.{mjs,cjs,js,mts,cts,ts,tsx}"],
"rules": {
"optimal-modules/no-named-exports": "off"
}
}
]
}
Rules
no-named-exports
Rule Prohibits using named exports for optimal module design.
Valid examples:
// No exports.
const a = true;
// Default export.
export default true;
// Default export.
const a = true;
export { a as default };
// TypeScript type default export.
type A = boolean;
export type { A as default };
Invalid examples:
// Named export.
export const a = true;
// Named export.
const a = true;
export { a };
// TypeScript type named export.
export type A = boolean;
To fix the above errors, move the thing being exported to its own module as a default export.
Configs
recommended
Config Enabled rules:
no-named-exports
(error).
Requirements
Supported runtime environments:
- Node.js versions
>=18.0.0
.
Projects must configure TypeScript to use types from the CommonJS modules that have a // @ts-check
comment:
compilerOptions.allowJs
should betrue
.compilerOptions.maxNodeModuleJsDepth
should be reasonably large, e.g.10
.compilerOptions.module
should be"node16"
or"nodenext"
.
Exports
These CommonJS modules are exported via the package.json
field exports
: