@putout/plugin-nodejs

putout plugin adds ability to transform code to new API of Node.js

Usage no npm install needed!

<script type="module">
  import putoutPluginNodejs from 'https://cdn.skypack.dev/@putout/plugin-nodejs';
</script>

README

@putout/plugin-nodejs NPM version

🐊Putout plugin adds ability to transform to new nodejs API and apply best practices.

Install

npm i putout @putout/plugin-nodejs -D

Options

{
    "rules": {
        "nodejs/convert-fs-promises": "on",
        "nodejs/convert-promisify-to-fs-promises": "on",
        "nodejs/convert-dirname-to-url": "on",
        "nodejs/convert-url-to-dirname": "on",
        "nodejs/convert-top-level-return": "on",
        "nodejs/remove-process-exit": "on"
    }
}

Rules

convert-fs-promises

Convert fs.promises into form that will be simpler to use and convert from in ESM to:

❌ Example of incorrect code

const {readFile} = require('fs').promises;

✅ Example of correct code

const {readFile} = require('fs/promises');

convert-promisify-to-fs-promises

❌ Example of incorrect code

const fs = require('fs');
const readFile = promisify(fs.readFile);

✅ Example of correct code

const {readFile} = require('fs/promises');

convert-dirname-to-url

Only for EcmaScript Modules.

❌ Example of incorrect code

import {readFile} from 'fs/promises';

const file1 = join(__dirname, '../../package.json');
const file2 = path.join(__dirname, '../../package.json');

✅ Example of correct code

import {readFile} from 'fs/promises';

const file1 = new URL('../../package.json', import.meta.url);
const file2 = new URL('../../package.json', import.meta.url);

convert-dirname-to-url

Only for CommonJS.

❌ Example of incorrect code

const {readFile} = require('fs/promises');
const file = new URL('../../package.json', import.meta.url);

✅ Example of correct code

const {readFile} = require('fs/promises');
const file = join(__dirname, '../../package.json');

remove-process-exit

In most cases process.exit() is called from bin directory, if not - disable this rule using match.

-process.exit();

convert-top-level-return

❌ Example of incorrect code

return;

✅ Example of correct code

process.exit();

declare

Add declarations to built-in node.js modules:

❌ Example of incorrect code

await readFile('hello.txt', 'utf8');

✅ Example of correct code

import {readFile} from 'fs/promises';
await readFile('hello.txt', 'utf8');

License

MIT