node.js - 如何使用“require”而不是“import”语句
问题描述
在 JavaScript、node.js 中,当我使用 CommonJS 样式导出模块时,例如
module.export.foo = (param) => {
// do sth here
return value
}
然后我开始foo
在我的 node.js 项目中输入另一个文件,VS Code 建议:“从 'path/to/file' 自动导入”按回车后,VS Code 在文件顶部插入语句:
import { foo } from 'path/to/file'
我希望 VS 代码改为粘贴以下代码:
const { foo } = require('path/to/file')
可能吗?
我的jsconfig.json
样子是这样的:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6"
},
"include": [
"src/**/*",
"__tests__/**/*"
]
}
解决方案
在 v1.46 中,这应该会更好:
CommonJS 自动导入
如果 VS Code 检测到您正在使用 CommonJS 样式的 JavaScript 模块,自动导入现在将使用
require
而不是import
.
推荐阅读
- pandas - 如何根据行的值将每一行乘以一个数字?
- tensorflow - 使用量化的 TFlite 权重运行推理时得到错误的结果
- reactjs - 添加两个 HOC 组件
不行 - python - 如何拆分字符串并获得首选值?
- mysql - JOIN 查询和 ANY 查询产生不同的结果
- javascript - 监听事件时推送数组
- python - KeyboardInterrupt 不能在父类方法中被捕获
- gradle - 如何在 gradle 任务中传递 nexus 凭据以下载工件
- python - 如何在 Python-Jupyter Notebooks 中为此条形图添加标题?
- firebase - 优化时间序列数据的 Firestore 成本?