当前位置:网站首页>require modular syntax
require modular syntax
2022-08-02 03:38:00 【cjx177187】
node.js环境中的全局函数:require
front-endjs文件用import
The backend runtime introduces a third partyjs文件 就用require()也可以用import(es6语法)
node.js中有3种模块
====>内置模块===>核心模块【node.jsIt is configured during installation】
var fs=require("fs")
====>第三方模块 【下载后使用】npm I 模块名
var mime=require("mime")
====>自己定义的模块
var mytool=require("mytool")
举例:
利用requireImport an own modulemytool,然后调用mytool模块中的tool函数
注意:Fill in the correct path,The suffix name is best written
代码:
var mytool=require("./mytool.js")
mytool.tool()
新建一个mytool.js在里面导入 what we want to calltool函数
代码:
module.exports={
tool(){
console.log(6666)
}
}
运行代码
require是导出,exports是导入
注意要和ES6in the import syntax to distinguish yo!
ES6===>export default{}
node.js====>module.exports={}
The difference between the three modules:
The built-in modules can be used directly without downloading in advance.Both built-in modules and third-party modules need to be downloaded,Otherwise, the small black window will report an error during runtime,Says the module is not found.When the third-party magic module is imported, fill in the required importjs文件的路径.
require(“fs”)Where to look for files?
去当前项目中的node_modules文件中找fs文件夹中的package.json中mainThe path corresponding to the field is then imported;没有就去nodeIt is also found in the global installation package path in the installation packagepackage.json中mainThe path corresponding to the field is then imported;如果还是没找到,It will go to the core library to show off and find it,If you still can't find it, you will get an error
流程:
- 当前项目的node_modules文件夹
- in the global installation packagenode_global文件夹===>node_modules文件夹
- in the global installation packagenode_modules文件夹===>npm===>node_modules文件夹[核心库]
边栏推荐
猜你喜欢
STM32 触发HardFault_Handler如何查找原因
yolov5调用ip摄像头时出现的问题
化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
PCL—point cloud data segmentation
Deveco studio Hongmeng app access network detailed process (js)
The @autowired distinguished from @ the Resource
mysql阶段总结
啃瓜记录又一天
新工程加载YOLOV6的预训练权重问题
DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶供应,MW:5000
随机推荐
ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全
js 数组去重的常用方法
npm和package.json
STM32 map文件解析
SOCKS5
npm--package.json---require
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
URL模块
A senior test engineer asked me these questions as soon as the interview came
C语言 十六进制整数字符串转十进制整数
JJWT tool class
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
parser = argparse.ArgumentParser()解析
@DateTimeFormat注解
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
mysql阶段总结
Basic usage of Monaco Editor
basic operator