当前位置:网站首页>3.nodejs--modularization
3.nodejs--modularization
2022-07-30 03:02:00 【love bubble tea】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
目录
1.模块化语法
(1)模块化概念
在nodejs中,应用由模块组成,nodejs中采用commonJS模块规范.
require语法:Encapsulate a series of similar functions into a module,然后供使用
(2)模块化分类
(1)内置模块/核心模块
由 node 本身提供,不需要单独安装(npm),可直接引入使用
例如:
var fs=require("fs")
(2)第三方模块
Provided by the community or individuals,需要通过npm安装后使用.It needs to be entered in the address bar of the foldercmd,Then type in the small black windownpm i mime下载.
例如:
var mine=require("mime")
(3)自定义模块
Created and defined by myself
注意:The path to the custom module must start with
./获取../开头,Followed by the suffix of the created file例如:
var myquerystring=require("./myquerystring.js")
2.模块的导入与导出
(1)模块导入
(1)通过
require("fs")来加载模块require("fs1") 解释: (1)去当前项目中的node_modules文件夹中找fs1文件夹 中的package.json中main字段 对应的路径 然后引入 (2)没有就去node的安装包中的全局安装路径中找 (3)还是没有找到 just go to the core library(2)如果是第三方模块,需要先使用npm进行下载
(3)如果是自定义模块,A relative path needs to be added
./或者../,可以省略.js后缀,如果文件名是index.js那么index.js也可以省略.(4)Modules can be loaded multiple times,But only on the first load
(2)模块导出
在模块的内部,
moduleThe variable represents the current module,它的exportsProperties are external interfaces,加载某个模块,加载的就是module.exports属性,This property points to an empty object.
3.npm和项目配置文件package.json文件
(1)npm
npm:Provides modules in the project/插件的 下载相关功能
(1)下载(常用)
npm i/install 模块名字/模块名字@版本号
npm i [email protected]
npm i 模块名 //安装到本地
npm i 模块名 -g//安装到全局
npm i 模块名 --save//安装到项目依赖
npm i 这个指令 It will go to the configuration file of the project to find the dependent packages 全部下载
npm run dev 这个指令 It will be found in the project's configuration filescripts字段 运行里面的dev指令
(2)更新/删除
npm update 模块名 //help you update to the latest version
npm update 模块名@版本 //Help you update to the specified version
npm uninstall 模块名 //删除模块
(2)package.json 文件
(1)每一个项目 构建时 先构建一个package.json文件 It represents some structure and configuration information for this project(项目的清单)
(2)构建指令: 在项目文件夹下 npm init -y 初始化A project manifest file npm i It is to install all dependencies according to the project manifest file
package.json 文件:项目的配置文件 Used to record some information about the entire project
项目名==>项目的名字
作者信息:
"scripts":{"dev":"node index.js"} 启动指令 主要用于小黑窗的npm启动 npm run dev
"dependencies" 项目依赖
边栏推荐
猜你喜欢
随机推荐
Redis(十) - Redission原理与实践
HCIP实验(05)OSPF综合实验
【科研工具的使用】A
1050 graphics card, why is the graphics card usage ranking on Steam always the top five
JUC(七):变量的线程安全分析
VMware磁盘扩容记录
CF1473C No More Inversions
houdini 使用HDA Processor 实现处理HDA输入输出
Leetcode.24 两两交换链表中的节点(递归)
【JS】iframe 嵌入页面用法
一个塑料瓶的海洋“奇幻漂流”
实现导入市场活动:
Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W
奥比中光高级副总裁王兆民离职 董事会秘书暂未取得资格证
Leetcode.234 判断回文链表(双指针/快慢指针)
开放地址法哈希实现——二次探测法
Successfully resolved AttributeError: 'PngImageFile' object has no attribute 'imshow'
Fudan-Washington University EMBA Kechuang's Ao E丨The Magical Materials and We Are Shaped
The relationship between the number of Oracle processes and the number of sessions
WebSocket在线通信









