Commit ce88625e authored by fireMan-34's avatar fireMan-34

📃 docs: nodejs 第三次相遇

parent 60210f4b
.DS_Store .DS_Store
\ No newline at end of file
**/node_modules
\ No newline at end of file
// 提供路径的操作函数
const { join } = require('path');
// process 进程 存储了 shell 执行进程的中的参数
const process = require('process');
// 提供文件处理函数
const { writeFile } = require('fs/promises');
const { myName, age } = require('./util');
// 在 node Js 有四个特殊的路径
// 分别是两个常量
// 当前脚步所在的文件夹路径
const dirname = __dirname;
const filename = __filename;
// 脚本执行路径
// 启动脚步的 shell 脚本所在文件夹路径
const cwd = process.cwd();
// 【node 执行程序路径, shell 脚本所在路径, ...剩余参数是脚本执行的参数】
// 例如 node index.js hellow
// hellow 就是 第三个匹配的参数,每一个空格匹配一个参数
const [ nodePath ] = process.argv;
/**
* join 语法可以拼接路径,将所有参数拼接成路径的字符串
*/
const tempJson = join(__dirname, 'temp.json');
console.log('临时文件路径', tempJson);
// 让我们创建一个临时的文件吧
writeFile(tempJson, JSON.stringify({ name: '临时文件' }), { encoding: 'utf-8' });
// 接着 node 启动一下改脚本
// 查看文件夹内的变化
// 做到这一步的话,恭喜你,你已经学会简单的读写,可能你还不太熟,但没有太多的关系,你已经入门了。接下来就是 api 的理解了
\ No newline at end of file
# nodeJs 模块化是什么呢。
# nodeJs 模块化是什么呢。
1. nodeJs 模块化是 nodeJs 提供的一种集成代码逻辑的能力。使得我们使用 node 执行脚本的时候可以把代码逻辑分散到各个文件,也可以获得 nodeJs api 的能力。
2. nodeJs 最初的模块是 CommonJs 规范,就是今天的主角 node 16.不得不说,node 迭代非常的快,目前已经默认使用 es 规范了。不同规范的包也导致了模块🌹解析之前的冲突。比方说 commonJs 规范的入口引入了 es 规范会导致 node 执行爆模块错误,通常来说会有个波浪线强调 import 的字段。通常来说确定模块化规范会根据 package 的 字段 、 如果是 ts 的 还有 tsconfig 字段来确定模块化规范。 node 执行的时候会依据当前目录最近的上一级的 package 文件,来确定模块化规范,如果没有的话,就是走 node 默认的模块化规范解析逻辑。
3. 说了这么多,让我们看看 CommonJs 的模块化规范吧
导入模块
```javascript
// Common JS module pattern
// 这些都是 nodeJs 内置的 api 模块
const path = require('path') // 相当于 es 规范的 import * as default 语法
const { writeFile } = require('fs'); // 相当于 import {} frojm 'xxx'
const axios = require('axios'); // 相当于我们 import node_module 下的模块
const 𝔘𝔫𝔦𝔠𝔬𝔡𝔢 = require('./animu-gentil'); // 相当于我们引入自己的模块
```
导出模块
```javascript
exports[属性|方法名] = ‘属性值|方法’
// 导出一个对象供外部使用
module.exports = {
}
```
好了,话不多说,简单实操一下。
1. 参考 `index.js` 文件, 在 `my.js` 操作
2. 使用`require` 引入模块解构出 join 方法(该方法是用来拼接路径)
3. 继续引入文件模块 fs ,仔细观察 `vscode` ,是否有 require 方法提示,有的话,恭喜你获取了 vscode 的智能提示可以更加愉快的编码工作。
没有的话,可以安装 node-snippet 或者在当前项目根目录安装 ts 提示 `npm install @types/node@16.x -D`
4. 记住由于现在模块规范在过度,往往市面上的 npm 包有可能是 es 规范,导致我们的 cjs 规范解析异常,所以可以尝试降级版本,每一次降级一个一个大版本。
如果是模块的模块版本异常,可以尝试手动修改包依赖版本,或者找更适合的包。(建议的话,还是找替代品或者看一下 issue 是否有解决方案)
5. 如果文件创建成功了,我们就进如下一个实操。
\ No newline at end of file
{
"name": "study3",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "study3",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@types/node": "^16.18.37"
}
},
"node_modules/@types/node": {
"version": "16.18.37",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.37.tgz",
"integrity": "sha512-ql+4dw4PlPFBP495k8JzUX/oMNRI2Ei4PrMHgj8oT4VhGlYUzF4EYr0qk2fW+XBVGIrq8Zzk13m4cvyXZuv4pA=="
}
},
"dependencies": {
"@types/node": {
"version": "16.18.37",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.37.tgz",
"integrity": "sha512-ql+4dw4PlPFBP495k8JzUX/oMNRI2Ei4PrMHgj8oT4VhGlYUzF4EYr0qk2fW+XBVGIrq8Zzk13m4cvyXZuv4pA=="
}
}
}
{
"name": "study3",
"version": "1.0.0",
"description": "学习nodeJs第三次相遇",
"main": "index.js",
"scripts": {
"start": "node index.js",
"start:my": "node my.js"
},
"author": "fire",
"license": "ISC",
"dependencies": {
"@types/node": "^16.18.37"
}
}
exports.myName = 'fire';
module.exports = {
age: 24,
}
\ No newline at end of file
# 简述 # 简述
...@@ -8,4 +8,6 @@ ...@@ -8,4 +8,6 @@
[ ] 阅读 nodeJs 简述![链接](./1/%E4%BB%80%E4%B9%88%E6%98%AFnodeJs.md) [ ] 阅读 nodeJs 简述![链接](./1/%E4%BB%80%E4%B9%88%E6%98%AFnodeJs.md)
## 2 ## 2
[ ] 打印属于你的世界 [ ] 打印属于你的世界
[ ] 挑战任务 [ ] 挑战任务
\ No newline at end of file ## 3
[ ] 创建一个json文件,并了解模块化的小知识
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment