ESLint plugin to enforce package.json type field to be either "module" or "commonjs".
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
Add the plugin to your ESLint configuration:
// eslint.config.js
import enforcePackageType from 'eslint-enforce-package-type';
import jsoncParser from 'jsonc-eslint-parser';
export default [
// Use recommended configuration (enforces "module" by default)
...enforcePackageType.configs.recommended,
// Or configure manually
{
files: ['package.json'],
plugins: {
'enforce-package-type': enforcePackageType
},
rules: {
// Default to 'module'
'enforce-package-type/enforce-package-type': 'error'
// Or enforce 'commonjs'
// 'enforce-package-type/enforce-package-type': ['error', { enforceType: 'commonjs' }]
},
languageOptions: {
parser: jsoncParser
}
}
];
Then run ESLint:
# Check for issues
npx eslint .
# Or automatically fix issues
npx eslint . --fix
This rule enforces that the type
field in package.json is set to either "module" or "commonjs".
The rule accepts an options object with the following properties:
enforceType
: The type to enforce ("module" or "commonjs"). Defaults to "module".
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
{
"name": "my-package",
"type": "module"
}
{
"name": "my-package",
"type": "commonjs"
}
{
"name": "my-package"
// Missing type field
}
{
"name": "my-package",
"type": "commonjs" // When enforceType is set to "module"
}
- ✅ 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
- Node.js >= 18
- npm >= 9
# Clone the repository
git clone https://github.com/MONEI/eslint-enforce-package-type.git
cd eslint-enforce-package-type
# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Lint files
npm run lint
# Lint and fix files
npm run lint:fix
# Format files
npm run format
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.
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
This project uses release-it for version management and package publishing.
Available commands:
# Dry run (no changes)
npm run release:dry
# Regular release
npm run release
# Alpha release
npm run release:alpha
# Beta release
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
- 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
MIT