Node.js fs API docs All In One
Node.js fs API docs All In One
Modules: node:module
API
CJS
// module.cjs
// In a CommonJS module
const builtin = require('node:module').builtinModules;
ESM
// module.mjs
// In an ECMAScript module
import { builtinModules as builtin } from 'node:module';
demo
import { isBuiltin } from 'node:module';
isBuiltin('node:fs'); // true
isBuiltin('fs'); // true
isBuiltin('wss'); // false
https://nodejs.org/api/module.html
fs
- old version
CJS
require
const fs = require('fs');
const path = require('path');
ESM
import
package.json
file config
{
// declare using ECMAScript modules(ESM)
"type": "module",
//...
}
import fs from 'fs';
import path from 'path';
- new version
ESM
import { existsSync } from 'node:fs';
https://nodejs.org/api/fs.html#fsexistssyncpath
path
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-11-11
* @modified
*
* @description CJS path
* @difficulty Easy
* @time_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
// export default ;
// export {
// ,
// };
const path = require('path');
const node_path = require('node:path');
log(`path =`, path)
log(`\nnode:path =`, node_path)
// $ node ./cjs-path.js
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-11-11
* @modified
*
* @description ESM path
* @difficulty Easy
* @time_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
// export default ;
// export {
// ,
// };
// (node:15850) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
import path from 'path';
import node_path from 'node:path';
log(`path =`, path)
log(`\nnode:path =`, node_path)
// $ node ./esm-path.mjs
https://nodejs.org/api/path.html
cjs
https://nodejs.org/api/modules.html
esm
https://nodejs.org/api/esm.html
refs
https://stackoverflow.com/a/71735771/5934465
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16882647.html
未经授权禁止转载,违者必究!