node.js - 如何修复“在索引 0 处找到带有非文字参数的 fs.readFile”?
问题描述
我正在尝试在 TypeScript 项目中添加eslint-plugin-security 。但是,对于这些代码
import { promises as fsp } from 'fs';
import fs from 'fs';
import path from 'path';
const index = await fsp.readFile(path.resolve(__dirname, './index.html'), 'utf-8');
const key = fs.readFileSync(path.join(__dirname, './ssl.key'));
await fsp.writeFile(path.resolve(__dirname, './sitemap.xml'), sitemap);
我收到了很多这样的 ESLint 警告:
warning Found fs.readFile with non literal argument at index 0 security/detect-non-literal-fs-filename warning Found fs.readFileSync with non literal argument at index 0 security/detect-non-literal-fs-filename warning Found fs.writeFile with non literal argument at index 0 security/detect-non-literal-fs-filename
我在https://github.com/nodesecurity/eslint-plugin-security#detect-non-literal-fs-filename找到了有关此 ESLint 错误的文档
但我仍然不知道如何解决它。任何指南都会有所帮助!谢谢
更新:
发现只要使用传递返回的路径path.join
就会path.resolve
出现这个ESLint问题。
如果我更改为绝对路径,ESLint 问题就消失了。path.join
但是,这通过or失去了相对路径的好处path.resolve
。
fs.readFileSync('/Users/me/project/ssl.key');
寻找替代/更好的方法(如果存在)。
(可能是误报?在https://github.com/nodesecurity/eslint-plugin-security/issues/65询问)
解决方案
推荐阅读
- ffmpeg - ffmpeg中的纯白HDR视频
- python - DistutilsArgError:未提供命令
- rust - `#[lang = "..."]` 属性有什么作用?
- python - 在枚举列表中过滤枚举 sqlalchemy
- colorbar - 如何为每个单独的频谱图修复 plt.colorbar() 的限制以进行比较
- java - 为什么在那之后我退出 Linux shell 中的 Spring Boot 应用程序(通过 CTRL+Z),一个进程仍在侦听 8080 端口?(我的应用程序使用的端口
- sql - 使用 TimescaleDB 获取每个组/聚合函数的最新/最新版本的版本化时间序列的有效方法
- qtableview - Quasar Q-Table 和数组中的数据(IndexedDB 和 Dexie)
- django - 如何通过 - 窗口函数过滤带注释的查询集 - 而不更改带注释字段的值
- python - python中的条件参数解析