【问题标题】:Convert require() without assignment to import将未赋值的 require() 转换为导入
【发布时间】:2022-01-19 11:19:12
【问题描述】:

file1.js

console.log("foo")

file2.js

require("./file1")
➜ node file2.js
foo

如何将 file2.js 的 require 转换为 import 并保持相同的逻辑(所以没有赋值)?

【问题讨论】:

    标签: javascript node.js ecmascript-6 es6-modules commonjs


    【解决方案1】:

    你可以像下面这样使用,这会将 file1 导入到 file2

    import './file1';
    

    当您想要执行 file1 代码但不想将其分配给变量时,这很有用。 例如:

    • 如果不需要连接对象,请创建数据库连接。
    • TypeScript 编译器生成的 JavaScript 完全没有 模块定义。

    【讨论】:

      猜你喜欢
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多