当前位置:网站首页>require模块化语法
require模块化语法
2022-08-02 03:22:00 【cjx177187】
node.js环境中的全局函数:require
引入前端的js文件用import
后端运行时引入第三方js文件 就用require()也可以用import(es6语法)
node.js中有3种模块
====>内置模块===>核心模块【node.js安装时就配置好的】
var fs=require("fs")
====>第三方模块 【下载后使用】npm I 模块名
var mime=require("mime")
====>自己定义的模块
var mytool=require("mytool")
举例:
利用require导入一个自己的模块mytool,然后调用mytool模块中的tool函数
注意:填写正确路径,后缀名最好的写上
代码:
var mytool=require("./mytool.js")
mytool.tool()
新建一个mytool.js在里面导入 我们想要调用的tool函数
代码:
module.exports={
tool(){
console.log(6666)
}
}
运行代码

require是导出,exports是导入
注意要和ES6中的导入语法进行区分哟!
ES6===>export default{}
node.js====>module.exports={}
三个模块的区别:
内置模块是不用提前下载的可以直接使用。内置模块和都三方模块都是需要下载的,不然运行时小黑窗会报错,说没有找到这个模块。第三方魔模块引入时要填写好需要引入的js文件的路径。
require(“fs”)会去哪里找文件?
去当前项目中的node_modules文件中找fs文件夹中的package.json中main字段对应的路径然后引入;没有就去node的安装包中的全局安装包路径中找也是找package.json中main字段对应的路径然后引入;如果还是没找到,就会去核心库里卖弄去找,还是没找到就会报错
流程:
- 当前项目的node_modules文件夹
- 全局安装包中的node_global文件夹===>node_modules文件夹
- 全局安装包中的node_modules文件夹===>npm===>node_modules文件夹[核心库]
边栏推荐
- 磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
- 【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
- sh: 1: curl: not found
- Redis笔记基础篇:6分钟看完Redis的八种数据类型
- 源码构建LAMP环境-1
- UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu
- 磷脂-聚乙二醇-醛基 DSPE-PEG-Aldehyde DSPE-PEG-CHO MW:5000
- Cloud server installation and deployment of Nacos 2.0.4 version
- [Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
- Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
猜你喜欢

How to check whether a table is locked in mysql

利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置

MySQL两阶段提交串讲

Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG

远程调试、无cuDnn、自定义模块无法导入问题记录

亚马逊卖家怎么提升转化率

STM32 触发HardFault_Handler如何查找原因

mysql卸载详细教程

Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields

PowerManagerService灭屏超时流程
随机推荐
Deveco studio 鸿蒙app访问网络详细过程(js)
C语言 0长度数组/柔性数组
querystring模块
MySQL分区表详解
C语言 void和void *(无类型指针)
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
MySQL分组后排序
Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
【装机】老毛桃的安装及使用
yolov5调用ip摄像头时出现的问题
A senior test engineer asked me these questions as soon as the interview came
Cut out web icons through PS 2021
[Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
磷脂-聚乙二醇-醛基 DSPE-PEG-Aldehyde DSPE-PEG-CHO MW:5000
ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全
@ApiModel 和 @ApiModelProperty
【博学谷学习记录】超强总结,用心分享 | 软件测试 接口测试基础
Problems when yolov5 calls ip camera
MySql创建数据表