eslint-enforce-package-type
ESLint plugin to enforce package.json type field to be either "module" or "commonjs".
Installation
First, install the required peer dependencies:
npm install --save-dev eslint@^9.0.0 jsonc-eslint-parser@^2.0.0
Then, install the plugin:
npm install --save-dev eslint-enforce-package-type
Usage
Add the plugin to your ESLint configuration:
import enforcePackageType from 'eslint-enforce-package-type';
import jsoncParser from 'jsonc-eslint-parser';
export default [
...enforcePackageType.configs.recommended,
{
files: ['package.json'],
plugins: {
'enforce-package-type': enforcePackageType
},
rules: {
'enforce-package-type/enforce-package-type': 'error'
},
languageOptions: {
parser: jsoncParser
}
}
];
Then run ESLint:
npx eslint .
npx eslint . --fix
Rule Details
This rule enforces that the type
field in package.json is set to either "module" or "commonjs".
Options
The rule accepts an options object with the following properties:
enforceType
: The type to enforce ("module" or "commonjs"). Defaults to "module".
Auto-fix
This rule supports the --fix
option. When enabled, it will:
- Add the
type
field if it's missing
- Change the
type
field value to match the enforced type
- Preserve all other fields and formatting
Examples
Valid
{
"name": "my-package",
"type": "module"
}
{
"name": "my-package",
"type": "commonjs"
}
Invalid
{
"name": "my-package"
}
{
"name": "my-package",
"type": "commonjs"
}
Features
- ✅ Enforces "type" field in package.json
- ✅ Supports both "module" and "commonjs" types
- ✅ Provides auto-fix functionality
- ✅ Configurable default type
- ✅ Ignores package.json files in node_modules
- ✅ Works with ESLint flat config
Development
Prerequisites
Setup
git clone https://github.com/MONEI/eslint-enforce-package-type.git
cd eslint-enforce-package-type
npm install
npm test
npm run test:watch
npm run lint
npm run lint:fix
npm run format
Git Hooks
This project uses husky and lint-staged to ensure code quality before commits:
- All staged files are formatted with Prettier
- JavaScript files are linted with ESLint
- All tests must pass
These checks run automatically before each commit.
Testing
Tests are written using Vitest and follow ESLint's testing conventions. Run tests with:
npm test
The test suite includes:
- Rule metadata validation
- Basic functionality tests
- Edge cases (malformed JSON, invalid types)
- Auto-fix functionality tests
Releasing
This project uses release-it for version management and package publishing.
Available commands:
npm run release:dry
npm run release
npm run release:alpha
npm run release:beta
The release process:
- Runs tests and linting
- Bumps version in package.json
- Creates a git tag
- Creates a GitHub release
- Publishes to npm
- Formats files after version bump
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
)
- Make your changes
- Run tests and linting
- Commit your changes (
git commit -m 'Add amazing feature'
)
- Push to the branch (
git push origin feature/amazing-feature
)
- Open a Pull Request
License
MIT