Socket
Book a DemoInstallSign in
Socket

eslint-enforce-package-type

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-enforce-package-type

ESLint plugin to enforce package.json type field (module or commonjs)

1.1.0
latest
Source
npmnpm
Version published
Maintainers
0
Created
Source

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:

// 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

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"
  // Missing type field
}
{
  "name": "my-package",
  "type": "commonjs" // When enforceType is set to "module"
}

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

  • Node.js >= 18
  • npm >= 9

Setup

# 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

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:

# 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

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

Keywords

eslint

FAQs

Package last updated on 10 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.