Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cjs-to-esm

npm npm downloads license

Easily convert your CommonJS (require, module.exports) code into modern ES Modules (import, export) with this simple CLI tool powered by jscodeshift.

✨ Features

✅ Converts module.exports to export default or named exports.
✅ Transforms require() statements into import statements.
✅ Handles default exports, named exports, and JSON imports.
✅ Resolves relative paths for modules correctly.
✅ Converts __dirname and __filename to ES module-compatible syntax.
✅ Supports both single file and batch processing

📦 Installation

npm install @ianfyan/cjs-to-esm -g

🚀 Usage

cjs-to-esm path/to/file.js

For example, to transform all files in the src/ directory:

cjs-to-esm src/

📖 Examples

🔹 Converting module.exports to export default

✅ Before (CommonJS)

const a = 1;
const b = 2;
module.exports = { a, b };

🔄 After (ES Modules)

const a = 1;
const b = 2;
export { a, b };

🔹 Converting require() to import

✅ Before (CommonJS)

const fs = require('fs');
const { Server } = require('socket.io');

🔄 After (ES Modules)

import fs from 'fs';
import socketIo from 'socket.io';

const { Server } = socketIo;

🔹 Handling __dirname and __filename

✅ Before (CommonJS)

const path = require('path');
const myDirname = path.resolve(__dirname, '../src/template');

🔄 After (ES Modules)

import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const myDirname = path.resolve(__dirname, '../src/template');

🔹 Converting JSON imports

✅ Before (CommonJS)

const packageJson = require('../package.json');

🔄 After (ES Modules)

import packageJson from '../package.json' with { type: 'json' };

🛠 Development

Clone the repository:

git clone https://github.com/ianfyan/cjs-to-esm.git
cd cjs-to-esm
npm install

💚 Run conversion

To test all functions:

npm convert path/to/file.js

📄 License

MIT License - Free to use and modify.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages