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.
npm i putout @putout/plugin-nodejs -D
{
"rules": {
"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-url-to-dirname": "on",
"nodejs/convert-top-level-return": "on",
"nodejs/declare": "on",
"nodejs/declare-after-require": "on",
"nodejs/remove-process-exit": "on"
}
}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.
const n = 100;
const buf = [];
new Buffer(123);
new Buffer(n);
new Buffer('hello');
new Buffer([]);
new Buffer(buf);const n = 100;
const buf = [];
Buffer.alloc(123);
Buffer.alloc(n);
Buffer.from('hello');
Buffer.from([]);
Buffer.from(buf);Convert fs.promises into form that will be simpler to use and convert to and from ESM.
const {readFile} = require('fs').promises;const {readFile} = require('fs/promises');const fs = require('fs');
const readFile = promisify(fs.readFile);const {readFile} = require('fs/promises');Only for ESM.
const {join} = require('path');
const path = require('path');
const file1 = join(__dirname, '../../package.json');
const file2 = path.join(__dirname, '../../package.json');const file1 = new URL('../../package.json', import.meta.url);
const file2 = new URL('../../package.json', import.meta.url);Only for CommonJS.
const {readFile} = require('fs/promises');
const file = new URL('../../package.json', import.meta.url);const {readFile} = require('fs/promises');
const {join} = require('path');
const file = join(__dirname, '../../package.json');In most cases process.exit() is called from bin directory, if not - disable this rule using match.
-process.exit();return;process.exit();Add declarations to built-in node.js modules:
Based on @putout/operator-declare.
await readFile('hello.txt', 'utf8');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"
]
}]
}
}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.
const name = 'hello.txt';
const {readFile} = require('fs/promises');const {readFile} = require('fs/promises');
const name = 'hello.txt';MIT