Skip to content

Commit

Permalink
docs: 更新nodejs知识点
Browse files Browse the repository at this point in the history
  • Loading branch information
simply-none committed May 8, 2024
1 parent 185e4d7 commit 3c5abb0
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion docs/usage-frame/nodejs/nodejs知识点.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
使用:当需要拼接一个路径的时候,有时会使用`path.join(__dirname, 'xxx')`进行拼接,也可以使用`require.resolve('xxx')`获取文件路径。它能够检查该路径是否存在,不存在则会抛出`cannot find xxx`的异常

三种方式:

```javascript
// 绝对路径 -> /Users/enhanced-resolve/lib/node.js
require.resolve('/Users/enhanced-resolve/')
Expand All @@ -29,11 +30,24 @@ require.resolve('diff')
```

例子:

```javascript
// 读取文件
// 使用path.join
fs.readFileSync(path.join(__dirname, './assets/some-file.txt'));

// 使用require.resolve
fs.readFileSync(require.resolve('./assets/some-file.txt'));
```
```

## 命令行`node`传参给脚本

使用node命令行执行脚本时,可以通过`process.argv`获取到传入的普通参数,通过`process.env['npm_config_xxx']`获取到传入的键值对参数

```javascript
// 执行node test.js 123
console.log(process.argv); // ['node', '/Users/xxx/test.js', '123']

// 执行node test.js --name=test
console.log(process.env); // { npm_config_name: 'test' }
```

0 comments on commit 3c5abb0

Please sign in to comment.