xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js fs API docs All In One

Node.js fs API docs All In One

image

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

  1. 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';

  1. 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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-11-12 18:23  xgqfrms  阅读(27)  评论(5编辑  收藏  举报