@putout/plugin-nodejs v16.4.0
@putout/plugin-nodejs 
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 -DRules
- β add-node-prefix;
- β add-missing-strict-mode;
- β convert-buffer-to-buffer-alloc;
- β convert-commonjs-to-esm-commons;
- β convert-commonjs-to-esm-exports;
- β convert-commonjs-to-esm-require;
- β convert-commonjs-to-esm.js;
- β convert-dirname-to-url;
- β convert-esm-to-commonjs;
- β convert-exports-to-module-exports;
- β convert-fs-promises;
- β convert-promisify-to-fs-promises;
- β convert-top-level-return;
- β convert-url-to-dirname;
- β declare;
- β declare-after-require;
- β group-require-by-id;
- β remove-process-exit;
- β remove-useless-promisify;
- β rename-file-cjs-to-js;
- β rename-file-mjs-to-js;
- β remove-useless-strict-mode;
- β remove-illegal-strict-mode;
- β cjs-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/group-require-by-id": "on",
"nodejs/remove-process-exit": "on",
"nodejs/add-missing-strict-mode": "on",
"nodejs/remove-useless-strict-mode": "on",
"nodejs/remove-illegal-strict-mode": "on",
"nodejs/remove-useless-promisify": "on"
}
}add-node-prefix
Denosupports using Node.js built-in modules such asfs,path,process, and many more vianode: 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 andnew 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
requirefunction is the easiest way to include modules that exist in separate files. The basic functionality ofrequireis that it reads a JavaScript file, executes the file, and then proceeds to return theexportsobject.(c) Nodejs.org
Check out in πPutout Editor.For ESM use esm/declare-imports-first.
β 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
importstatement can be used to reference a value exposed by theexportstatement 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);group-require-by-id
Checkout in πPutout Editor. For ESM use esm/group-imports-by-sources.
β Example of incorrect code
const ss = require('../../bb/ss');
const d = require('../hello');
const react = require('react');
const {lodash} = require('lodash');
const fs = require('node:fs');
const b = require('./ss');
const m = require(x);
const c = 5;β Example of correct code
const fs = require('node:fs');
const react = require('react');
const {lodash} = require('lodash');
const ss = require('../../bb/ss');
const d = require('../hello');
const b = require('./ss');
const m = require(x);
const c = 5;convert-esm-to-commonjs
CommonJS is a module system supported in Node, it provides a
requirefunction, which can be used to access theexportsobject 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.jsCheck out in πPutout Editor.
rename-file-mjs-to-js
Rename *.mjs files when type === "module":
/
|-- package.json
`-- lib/
- `-- hello.mjs
+ `-- hello.jsCheck 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-illegal-strict-mode
SyntaxError: "use strict" not allowed in function with non-simple parametersThe JavaScript exception"use strict" not allowed in functionoccurs when ause strictdirective is used at the top of a function with default parameters, rest parameters, or destructuring parameters.(c) MDN
Checkout in πPutout Editor.
β Example of incorrect code
function x1(...a) {
'use strict';
}
function x2(a, b = 3) {
'use strict';
}
function x3({a}) {
'use strict';
}
function x4([a]) {
'use strict';
}
function x5(...a) {
'use strict';
}β Example of correct code
function x1(...a) {}
function x2(a, b = 3) {}
function x3({a}) {}
function x4([a]) {}
function x5(...a) {}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
7 months ago
8 months ago
11 months ago
9 months ago
11 months ago
6 months ago
6 months ago
8 months ago
8 months ago
8 months ago
9 months ago
9 months ago
11 months ago
6 months ago
7 months ago
5 months ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago