@putout/plugin-convert-commonjs-to-esm
🐊Putout
plugin adds ability to convert Commonjs to ESM.
Install
npm i @putout/plugin-convert-commonjs-to-esm -D
Rule
{
"rules": {
"convert-commonjs-to-esm/require": "on",
"convert-commonjs-to-esm/exports": "on",
"convert-commonjs-to-esm/commons": "on"
}
}
require
❌ Incorrect code example
const {join} = require('path');
const args = require('minimist')({
string: ['a', 'b'],
});
✅ Correct code Example
import {join} from 'path';
import minimist from 'minimist';
const args = minimist({
string: ['a', 'b'],
});
exports
❌ Incorrect code example
module.exports = () => {
};
✅ Correct code Example
export default () => {
};
Commons
❌ Incorrect code example
const {readFile} = require('fs/promises');
await readFile(__filename);
✅ Correct code Example
import {readFile} from 'fs/promises';
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
await readFile(__filename);
License
MIT