Common JavaScript File Extensions
The `common-js-file-extensions` package provides two static arrays, `code` and `config`, listing various file extensions associated with JavaScript code and configuration files, respectively. It includes extensions for a wide range of JavaScript dialects and transpiled languages, such as standard `.js`, `.mjs`, `.cjs`, as well as older or less common ones like `.coffee`, `.ts`, `.iced`, `.es6`, `.jsx`, and `.json5`. Currently at version 1.0.4, the package appears to be stable but likely no longer actively maintained, given its last update years ago (copyright 2016). Its primary utility lies in offering a comprehensive, if somewhat dated, reference for tools needing to identify JavaScript-related files without needing to parse package manifests or project configurations for specific language tooling.
Common errors
-
TypeError: Cannot destructure property 'code' of 'common_js_file_extensions_1' as it is undefined.
cause Attempting a named ES Module import (`import { code } from 'common-js-file-extensions';`) of a CommonJS module in a Node.js ESM environment.fixImport the module's default export first, then destructure from it: `import extensions from 'common-js-file-extensions'; const { code, config } = extensions;` -
ReferenceError: require is not defined
cause Attempting to use `require()` syntax in an ES Module (ESM) file without proper transpilation or configuration.fixIf your project is ESM-first, use `import` syntax: `import extensions from 'common-js-file-extensions'; const { code, config } = extensions;`. If you must use `require` in an ESM file, you might need to use `createRequire` from the `module` built-in module, but it's generally recommended to stick to `import`.
Warnings
- gotcha This package is explicitly a CommonJS module. While it can be imported into an ES Module (ESM) context, direct named imports (`import { code } from 'pkg'`) will not work as expected in Node.js, often resulting in `undefined` or runtime errors. ESM users must import the module's default export (which is the CommonJS `module.exports` object) and destructure from that.
- gotcha The package includes a very comprehensive list of extensions, encompassing many older, niche, or less common JavaScript dialects and transpilers (e.g., IcedCoffeeScript, Stratified JavaScript, LiveScript, Earl Grey). While thorough, this breadth might not be relevant for most modern JavaScript projects and could lead to over-inclusive file scanning or unnecessary complexity if used without filtering.
- deprecated The package has not seen updates since its initial releases around 2016 (indicated by copyright and versioning). This suggests it is no longer actively maintained. While its static data is unlikely to 'break', new or evolving JavaScript file extensions (e.g., future TypeScript or JSX variants, or new bundler-specific extensions) will not be added.
Install
-
npm install common-js-file-extensions -
yarn add common-js-file-extensions -
pnpm add common-js-file-extensions
Imports
- code, config
import { code, config } from 'common-js-file-extensions';const { code, config } = require('common-js-file-extensions'); - All exports as default
import * as extensions from 'common-js-file-extensions';
import extensions from 'common-js-file-extensions'; const { code, config } = extensions;
Quickstart
const { code, config } = require('common-js-file-extensions');
console.log('--- JavaScript Code File Extensions ---');
code.forEach(ext => console.log(` .${ext}`));
console.log('\n--- JavaScript Config/Object File Extensions ---');
config.forEach(ext => console.log(` .${ext}`));
// Example of checking a file extension
const fileName = 'my-component.tsx';
const fileExtension = fileName.split('.').pop();
if (fileExtension && code.includes(fileExtension)) {
console.log(`\n'${fileName}' is a known JavaScript code file.`);
} else if (fileExtension && config.includes(fileExtension)) {
console.log(`\n'${fileName}' is a known JavaScript config/object file.`);
} else {
console.log(`\n'${fileName}' is not recognized as a common JS file type.`);
}