Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

mjs and cjs extensions not detected by default #733

@ericcornelissen

Description

@ericcornelissen

Bug Description

depcheck does not seem to recognize JavaScript files using the file extensions .mjs and .cjs by default. These file extensions are used to allow writing JavaScript with ESModule syntax in CommonJS projects (.mjs) or JavaScript with CommonJS syntax in ESModule projects (.cjs). You can read more about this in the Node.js documentation, see ECMAScript modules.

I would expect depcheck to recognize files using .mjs and .cjs by default, rather then having to configure it along the lines of:

{
  // Other configuration...
  "parsers": {
    "**/*.js": "es6",
    "**/*.cjs": "es6",
    "**/*.mjs": "es6"
  }
}

This problem was raised before in #615 and #669, but has not been addressed beyond recommendations to use the configuration.

Code snippets

See https://github.com/ericcornelissen/depcheck-bugs/tree/e29920d7d63b14b3825a9f83301a7726dd312dbc/mjs-and-cjs (the specific folder in the repository) for a minimum project reproducing the problem.

1. .mjs

Where the dependency is used:

// ./index.mjs

import test from 'ava';

test('my passing test', t => {
	t.pass();
});

Where the dependency is listed in package.json:

{
  "name": "depcheck-with-mjs-and-cjs",
  "version": "0.0.1",
  "description": "Example of depcheck not detecting dependencies used in .mjs and .cjs files",
  "private": true,
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "vet": "depcheck"
  },
  "author": "Eric Cornelissen",
  "license": "Unlicense",
  "dependencies": {
    "ava": "4.3.0", // <--- Here
    "dotenv": "16.0.1"
  },
  "devDependencies": {
    "depcheck": "1.4.3"
  },
  "engine": {
    "node": "^14.18.0 || ^16.0.0 || ^18.0.0"
  }
}

1. .cjs

Where the dependency is used:

// ./index.cjs

require("dotenv").config();

console.log(`Hello ${process.env.name}!`);

Where the dependency is listed in package.json:

{
  "name": "depcheck-with-mjs-and-cjs",
  "version": "0.0.1",
  "description": "Example of depcheck not detecting dependencies used in .mjs and .cjs files",
  "private": true,
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "vet": "depcheck"
  },
  "author": "Eric Cornelissen",
  "license": "Unlicense",
  "dependencies": {
    "ava": "4.3.0",
    "dotenv": "16.0.1" // <--- Here
  },
  "devDependencies": {
    "depcheck": "1.4.3"
  },
  "engine": {
    "node": "^14.18.0 || ^16.0.0 || ^18.0.0"
  }
}

Versions

  • node -v: v14.17.6
  • npm -v: 6.14.15
  • depcheck --version: 1.4.3

Extra info

Output of depcheck --json

{
  "dependencies": [
    "ava",
    "dotenv"
  ],
  "devDependencies": [],
  "missing": {},
  "using": {
    "depcheck": [
      "/path/to/mjs-and-cjs/package.json"
    ]
  },
  "invalidFiles": {},
  "invalidDirs": {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions