@putout/plugin-nodejs
🐊Putout plugin adds ability to transform code to new API of Node.js
Last updated 2 years ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/plugin-nodejs 
SYNC missed versions from official npm registry.

@putout/plugin-nodejs NPM version

Node.js is an open-source, cross-platform, JavaScript runtime environment.

(c) Nodejs.org

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

Install

npm i putout @putout/plugin-nodejs -D

Rules

  • βœ… [add-node-prefix][#add-node-prefix];
  • βœ… [convert-buffer-to-buffer-alloc][#convert-buffer-to-buffer-alloc];
  • βœ… [convert-commonjs-to-esm-commons][#convert-commonjs-to-esm-commons];
  • βœ… [convert-commonjs-to-esm-exports][#convert-commonjs-to-esm-exports];
  • βœ… [convert-commonjs-to-esm-require][#convert-commonjs-to-esm-require];
  • βœ… [convert-commonjs-to-esm.js][#convert-commonjs-to-esm.js];
  • βœ… [convert-dirname-to-url][#convert-dirname-to-url];
  • βœ… [convert-esm-to-commonjs][#convert-esm-to-commonjs];
  • βœ… [convert-exports-to-module-exports][#convert-exports-to-module-exports];
  • βœ… [convert-fs-promises][#convert-fs-promises];
  • βœ… [convert-promisify-to-fs-promises][#convert-promisify-to-fs-promises];
  • βœ… [convert-top-level-return][#convert-top-level-return];
  • βœ… [convert-url-to-dirname][#convert-url-to-dirname];
  • βœ… [declare][#declare];
  • βœ… [declare-after-require][#declare-after-require];
  • βœ… [remove-process-exit][#remove-process-exit];
  • βœ… [remove-useless-promisify][#remove-useless-promisify];
  • βœ… [rename-file-cjs-to-js][#rename-file-cjs-to-js];
  • βœ… [rename-file-mjs-to-js][#rename-file-mjs-to-js];
  • βœ… [strict-mode][#strict-mode];
  • βœ… [cjs-file][#cjs-file];
  • βœ… [mjs-file][#mjs-file];

Config

{
    "rules": {
        "nodejs/convert-commonjs-to-esm": "off",
        "nodejs/convert-esm-to-commonjs": "off",
        "nodejs/cjs-file": "off",
        "nodejs/mjs-file": "off",
        "nodejs/rename-file-cjs-to-js": "off",
        "nodejs/rename-file-mjs-to-js": "off",
        "nodejs/add-node-prefix": "on",
        "nodejs/convert-buffer-to-buffer-alloc": "on",
        "nodejs/convert-fs-promises": "on",
        "nodejs/convert-promisify-to-fs-promises": "on",
        "nodejs/convert-dirname-to-url": "on",
        "nodejs/convert-exportst-to-module-exports": "on",
        "nodejs/convert-url-to-dirname": "on",
        "nodejs/convert-top-level-return": "on",
        "nodejs/declare": "on",
        "nodejs/declare-after-require": "on",
        "nodejs/remove-process-exit": "on",
        "nodejs/add-missing-strict-mode": "on",
        "nodejs/remove-useless-strict-mode": "on",
        "nodejs/remove-useless-promisify": "on"
    }
}

add-node-prefix

Deno supports using Node.js built-in modules such as fs, path, process, and many more via node: specifiers.

(c) deno.land

Check out in 🐊Putout Editor.

❌ Example of incorrect code

import fs from 'fs';

const path = require('path');
await import('path');

βœ… Example of correct code

import fs from 'node:fs';

const path = require('node:path');
await import('node:path');

Comparison

Linter Rule Fix
🐊 Putout apply-node-prefix βœ…
⏣ ESLint prefer-node-protocol βœ…

convert-buffer-to-buffer-alloc

The Buffer() function and new Buffer() constructor are deprecated due to API usability issues that can lead to accidental security issues.

(c) DEP0005

Check out in 🐊Putout Editor.

❌ Example of incorrect code

const n = 100;
const buf = [];

new Buffer(123);
new Buffer(n);
new Buffer('hello');

new Buffer([]);
new Buffer(buf);

βœ… Example of correct code

const n = 100;
const buf = [];

Buffer.alloc(123);
Buffer.alloc(n);
Buffer.from('hello');

Buffer.from([]);
Buffer.from(buf);

convert-fs-promises

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

❌ 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 ESM.

❌ Example of incorrect code

const {join} = require('path');
const path = require('path');

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

βœ… Example of correct code

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

convert-url-to-dirname

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 {join} = require('path');
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-exports-to-module-exports

Since exports = 5 wan't make any export, just change value of variable. Checkout in 🐊Putout Editor.

❌ Example of incorrect code

exports.x = 5;

βœ… Example of correct code

module.exports.x = 5;

convert-top-level-return

❌ Example of incorrect code

return;

βœ… Example of correct code

process.exit();

declare

Add declarations to built-in node.js modules:

Based on @putout/operator-declare.

❌ Example of incorrect code

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

βœ… Example of correct code

import {readFile} from 'fs/promises';

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

When you want to skip some declaration use dismiss:

{
    "rules": {
        "nodejs/declare": ["on", {
            "dismiss": ["readFile"]
        }]
    }
}

declare-after-require

Node.js follows the CommonJS module system, and the builtin require function is the easiest way to include modules that exist in separate files. The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object.

(c) Nodejs.org

Check out in 🐊Putout Editor.

❌ Example of incorrect code

const name = 'hello.txt';
const {readFile} = require('fs/promises');

βœ… Example of correct code

const {readFile} = require('fs/promises');
const name = 'hello.txt';

convert-commonjs-to-esm

Convert CommonJS EcmaScript Modules.

EcmaScript module syntax is the standard way to import and export values between files in JavaScript. The import statement can be used to reference a value exposed by the export statement in another file.

(c) parceljs

require

❌ Example of incorrect code

const {join} = require('path');

const args = require('minimist')({
    string: ['a', 'b'],
});

βœ… Example of correct code

import {join} from 'path';
import minimist from 'minimist';

const args = minimist({
    string: ['a', 'b'],
});

exports

❌ Example of incorrect code

module.exports = () => {};

βœ… Example of correct code

export default () => {};

Commons

❌ Example of incorrect code

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

await readFile(__filename);

βœ… Example of correct code

import {readFile} from 'fs/promises';
import {fileURLToPath} from 'url';

const __filename = fileURLToPath(import.meta.url);
await readFile(__filename);

convert-esm-to-commonjs

CommonJS is a module system supported in Node, it provides a require function, which can be used to access the exports object exposed by another file.

(c) parceljs

Convert EcmaScript Modules to CommonJS.

❌ Example of incorrect code

import hello from 'world';

βœ… Example of correct code

const hello = require('world');

cjs-file

Run convert-esm-to-commonjs for all *.cjs files with help of redlint.

Check out in 🐊Putout Editor.

mjs-file

Run convert-commonjs-to-esm for all *.cjs files with help of redlint.

Check out in 🐊Putout Editor.

rename-file-cjs-to-js

Rename *.cjs files when type === "commonjs":

 /
 |-- package.json
 `-- lib/
-     `-- hello.cjs
+     `-- hello.js

Check out in 🐊Putout Editor.

rename-file-mjs-to-js

Rename *.mjs files when type === "module":

 /
 |-- package.json
 `-- lib/
-     `-- hello.mjs
+     `-- hello.js

Check out in 🐊Putout Editor.

add-missing-strict-mode

Strict mode makes several changes to normal JavaScript semantics:

  • Eliminates some JavaScript silent errors by changing them to throw errors.
  • Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
  • Prohibits some syntax likely to be defined in future versions of ECMAScript.

(c) MDN

Add strict mode to CommonJS:

❌ Example of incorrect code

const a = require('b');

βœ… Example of correct code

'strict mode';

const a = require('b');

βœ… Example of correct code

remove-useless-strict-mode

Remove 'use strict' from ESM.

❌ Example of incorrect code

'strict mode';

import a from 'b';

βœ… Example of correct code

import a from 'b';

remove-useless-promisify

Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.

(c) nodejs.org

Remove useless promisify(). Checkout in 🐊Putout Editor.

❌ Example of incorrect code

export const readSize = promisify(async (dir, options, callback) => {});

βœ… Example of correct code

export const readSize = async (dir, options, callback) => {};

License

MIT

Current Tags

  • 11.11.0                                ...           latest (6 months ago)

61 Versions

  • 11.11.0                                ...           6 months ago
  • 11.10.0                                ...           6 months ago
  • 11.9.1                                ...           7 months ago
  • 11.9.0                                ...           7 months ago
  • 11.8.0                                ...           7 months ago
  • 11.7.1                                ...           7 months ago
  • 11.7.0                                ...           7 months ago
  • 11.6.0                                ...           7 months ago
  • 11.5.0                                ...           8 months ago
  • 11.4.0                                ...           8 months ago
  • 11.3.0                                ...           8 months ago
  • 11.2.0                                ...           8 months ago
  • 11.1.0                                ...           8 months ago
  • 11.0.0                                ...           9 months ago
  • 10.3.0                                ...           a year ago
  • 10.1.1                                ...           a year ago
  • 10.1.0                                ...           a year ago
  • 10.0.3                                ...           a year ago
  • 10.0.2                                ...           a year ago
  • 10.0.1                                ...           a year ago
  • 10.0.0                                ...           a year ago
  • 9.4.0                                ...           a year ago
  • 9.3.0                                ...           a year ago
  • 9.2.0                                ...           a year ago
  • 9.1.3                                ...           a year ago
  • 9.1.2                                ...           a year ago
  • 9.1.1                                ...           a year ago
  • 9.1.0                                ...           a year ago
  • 9.0.1                                ...           a year ago
  • 9.0.0                                ...           a year ago
  • 8.1.0                                ...           a year ago
  • 8.0.0                                ...           a year ago
  • 7.2.0                                ...           a year ago
  • 7.1.0                                ...           a year ago
  • 7.0.0                                ...           a year ago
  • 6.0.1                                ...           a year ago
  • 6.0.0                                ...           2 years ago
  • 5.0.0                                ...           2 years ago
  • 4.8.1                                ...           2 years ago
  • 4.8.0                                ...           2 years ago
  • 4.7.1                                ...           2 years ago
  • 4.7.0                                ...           2 years ago
  • 4.6.3                                ...           2 years ago
  • 4.6.2                                ...           2 years ago
  • 4.6.1                                ...           2 years ago
  • 4.6.0                                ...           2 years ago
  • 4.5.0                                ...           2 years ago
  • 4.4.0                                ...           2 years ago
  • 4.3.0                                ...           3 years ago
  • 4.2.0                                ...           3 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (11)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE