当前位置: 首页 > news >正文

【Node.js】module 模块化

认识 node.js

Node.js 是一个独立的 JavaScript 运行环境,能独立执行 JS 代码,可以用来编写服务器后端的应用程序。基于Chrome V8 引擎封装,但是没有 DOM 和 BOM。Node.js 没有图形化界面。node -v 检查是否安装成功。node index.js 执行该文件夹下的 index.js 文件。

modules 模块化

commonJS 写法

// a.js
const Upper = (str) => {return str.substring(0,1).toUpperCase() + str.substring(1)
}
const fn = () => {console.log("this is a")
}
// 接口暴露方法一:
// module.exports = {
//   upper: Upper,
//   fn
// }
// 接口暴露方法二:
exports.upper = Upper
exports.fn = fn
// index.js
const A = require('./modules/a')
A.fn()  // this is a
console.log(A.upper('hello'))  // Hello

ES 写法

需要先 npm install 安装依赖,生成 node_modules 文件夹,然后在 package.json 中配置 "type": "module",,之后才可以使用这种写法。

// a.js
const Upper = (str) => {return str.substring(0,1).toUpperCase() + str.substring(1)
}
const fn = () => {console.log("this is a")
}
// 接口暴露方法一:
// module.exports = {
//   Upper,
//   fn
// }
// 接口暴露方法二:
// exports.upper = Upper
// exports.fn = fn
// 接口暴露方法三:
export {Upper,fn
}
// index.js
// const fnn = require('./modules/a')
// 注意:此时导入a.js 文件必须加上 js 后缀
import { Upper } from './modules/a.js'
console.log(Upper('hello'))  // Hello
http://www.lryc.cn/news/187058.html

相关文章:

  • Vue中如何进行分布式日志收集与日志分析(如ELK Stack)
  • java学习--day23(线程池)
  • Unity Golang教程-Shader编写一个流动的云效果
  • Python数据攻略-Pandas与地理空间数据分析
  • sourceTree无法启动
  • 【ARM Coresight 系列文章19 -- Performance Monitoring Unit(性能监测单元)
  • 前端学习| 第二章
  • Unity中Shader光强与环境色
  • Android9 查看连接多个蓝牙耳机查看使用中的蓝牙耳机
  • 【EF Core】如何忽略影响导航属性(级联)
  • 【苍穹外卖 | 项目日记】第一天
  • WuThreat身份安全云-TVD每日漏洞情报-2023-10-07
  • SpringBoot整合Druid配置yml文件
  • 如何保证 RabbitMQ 的消息可靠性?
  • 图像分类数据集划分(创建ImageNet格式的数据集)
  • ArcGIS Engine:报错无法嵌入互操作类型“ESRI.ArcGIS.Geometry.EnvelopeClass”。请改用适用的接口。
  • 核货宝:服装店收银系统如何选择?收银系统选购指南!
  • GB/T 7134-2008 浇筑型工业有机玻璃板材检测
  • 数据采集平台(二)
  • Nginx + PHP 异常排查,open_basedir 异常处理
  • Linux免密登录
  • 迷宫 蓝桥杯
  • 25 mysql like 是否使用索引
  • Android---Class 对象在执行引擎中的初始化过程
  • Altium Designer实用系列(二)----PCB绘图小技巧
  • threejs-开发入门与调试设置
  • win11安装双系统Ubuntu的坎坷记录
  • 关于docker的xuexi
  • Python接口自动化测试实战详解,你想要的全都有
  • SparkSQL 外部数据源