首页 > 编程语言 >[VueJsDev] 基础知识 - Node.js常用函数

[VueJsDev] 基础知识 - Node.js常用函数

时间:2023-01-09 16:35:29浏览次数:59  
标签:Node resolve const stream js path import VueJsDev dirname

[VueJsDev] 目录列表
https://www.cnblogs.com/pengchenggang/p/17037320.html

Node.js 常用函数

总结常用 node 函数 用的 ESM 模块。

// package.json
"type": "module",

Func. 1: 读取文件-同步/异步

读取 path 文件 ESM模式

  • 同步读取文件
import { readFileSync } from "fs"
import { fileURLToPath } from "url"
import { dirname, resolve } from "path"

const __dirname = dirname(fileURLToPath(import.meta.url))
const path = resolve(__dirname, "path/data.json")
const data = readFileSync(path, { encoding: "utf-8" })
console.log(data)
  • 异步读取文件 - 异步函数可以配合 getAc
import { readFile } from "fs"
import { fileURLToPath } from "url"
import { dirname, resolve } from "path"

const __dirname = dirname(fileURLToPath(import.meta.url))
const path = resolve(__dirname, "path/data.json")
readFile(path, { encoding: "utf-8" }, (err, data) => {
  if (!err) {
    console.log(data)
  } else {
    console.error(err)
  }
})

Func. 2: 写入文件-同步

import { writeFileSync, mkdirSync, existsSync } from "fs"
import { fileURLToPath } from "url"
import { dirname, resolve } from "path"
const __dirname = dirname(fileURLToPath(import.meta.url))
const title = '111'
const outputDir = resolve(__dirname, "output")
const outputFile = resolve(outputDir, `${title}.txt`)

// 检查outputDir是否存在,没有则创建一个
if (!existsSync(outputDir)) {
  mkdirSync(outputDir)
}

const text = `123`
writeFileSync(outputFile, text) // 将text写入outputFile文件中

return outputFile

Para. 3: 入参 process.argv

脚本接收的参数 以数组的形式接收

console.info('process.argv', process.argv)

Func. 4: 查看module全部模块

查看node环境 全部模块

// commonjs
const {builtinModules} = require('module');
console.log(builtinModules);
// esm
import {builtinModules} from 'module'
console.log(builtinModules);
// output
[
  '_http_agent',         '_http_client',        '_http_common',
  '_http_incoming',      '_http_outgoing',      '_http_server',
  '_stream_duplex',      '_stream_passthrough', '_stream_readable',
  '_stream_transform',   '_stream_wrap',        '_stream_writable',
  '_tls_common',         '_tls_wrap',           'assert',
  'assert/strict',       'async_hooks',         'buffer',
  'child_process',       'cluster',             'console',
  'constants',           'crypto',              'dgram',
  'diagnostics_channel', 'dns',                 'dns/promises',
  'domain',              'events',              'fs',
  'fs/promises',         'http',                'http2',
  'https',               'inspector',           'module',
  'net',                 'os',                  'path',
  'path/posix',          'path/win32',          'perf_hooks',
  'process',             'punycode',            'querystring',
  'readline',            'repl',                'stream',
  'stream/consumers',    'stream/promises',     'stream/web',
  'string_decoder',      'sys',                 'timers',
  'timers/promises',     'tls',                 'trace_events',
  'tty',                 'url',                 'util',
  'util/types',          'v8',                  'vm',
  'worker_threads',      'zlib'
]

标签:Node,resolve,const,stream,js,path,import,VueJsDev,dirname
From: https://www.cnblogs.com/pengchenggang/p/17037416.html

相关文章

  • [VueJsDev] 基础知识 - Button的全局节流
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.htmlButton的全局节流:::details目录目录Button的全局节流Step.1:注册函数Step.2:局部......
  • [VueJsDev] 基础知识 - asyncTool.js异步执行工具
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.htmlasyncTool.js异步执行工具:::details目录目录asyncTool.js异步执行工具Step.1:getA......
  • [VueJsDev] 其他知识 - 单词本
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.html单词本z这里的单词就是很随性的记忆,来源有生活中能见到的,或者抖音见到的等等~~:::detai......
  • [VueJsDev] 其他知识 - NestJS 学习内容
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.htmlNestJS学习内容NestJS学习总结Step.1:全局安装包pnpmadd-g@nestjs/clinodemon......
  • [VueJsDev] 目录列表
    [VueJsDev]目录列表云服务器域名就一年,gitee上有不给发布,没办法https://www.vuejsdev.com/还是迁移到博客园吧。目录列表快速入门开发前小知识vue项目根目录配置......
  • [VueJsDev] 日志 - nginxConfig 配置文件备份
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.htmlnginxConfig配置文件备份:::details目录目录nginxConfig配置文件备份Step.1:服务器......
  • [VueJsDev] 日志 - BBTime-LOG
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.htmlBBTime-LOG:::details目录目录BBTime-LOGLog.1:AutoNumber成功发布到vscode插件市场......
  • JS加载层
    昨天有个项目要用到加载层,寻思了一下,框架的加载层也就那样,频繁自己写也不是事。所以花了些时间封装了一个JS类,内置9种图标样式,全局主要样式可自定义。转载请附上本文链接......
  • vite.config.js 无法使用__dirname的解决方法
    __dirname是commonjs规范的内置变量。如果使用了esm是不会自动注入这个变量的。在commonjs中,注入了__dirname,__filename,module,exports,require五个内置变量用于实......
  • JSON to TS 转化 Axios 请求类型约束
    JSONtoTS扩展插件的使用作用场景作用是快速转化React+TS的Axios拿回数据的类型约束定义 步骤一.获取数据当页面中发起请求成功,获取到接口所携带的数据时,cons......