【问题标题】:How to use 'require' instead of 'import' statements如何使用“require”而不是“import”语句
【发布时间】:2020-04-12 21:46:11
【问题描述】:

在JavaScript,node.js中,当我使用CommonJS风格导出模块时,比如

module.export.foo = (param) => {
    // do sth here
   return value
}

然后我开始在我的 node.js 项目的另一个文件中输入foo,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__/**/*"
    ]
  }

【问题讨论】:

  • 根据issue #26,目前似乎无法将 TS/JS 的自定义模型训练到 VS Code 的 IntelliCode 扩展。

标签: node.js visual-studio-code node-modules


【解决方案1】:

在 v1.46 中这应该会更好:

CommonJS 自动导入

如果 VS Code 检测到您正在使用 CommonJS 样式的 JavaScript 模块,自动导入现在将使用 require 而不是 import

来自v1.46 release notes: CommonJS imports

【讨论】:

猜你喜欢
  • 2019-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2019-09-23
  • 2021-10-28
  • 1970-01-01
相关资源
最近更新 更多