typescript - 以 ECMAScript 6 或更高版本为目标时,不能使用“require”导入
问题描述
在 VS2019 16.2.1 中,我使用带有 TypeScript 模板的 Basic Azure Node.js Express 4 Application 创建了一个新项目。
我可以构建和运行应用程序。
但是,在 app.ts 中,如果我将鼠标悬停在这条线上
import debug = require('debug');
我看到一个工具提示
Import with 'require' cannot be used when targeting ECMAScript 6 or higher
这是 tsconfig.json
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": ["es6"],
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
为什么我会看到此消息,我应该怎么做?
[更新]
我尝试将第一行更改为
从“调试”导入调试;
然后我得到构建错误
app.ts(1,8): error TS1259: Module '"C:/Users/kirst/source/repos/ExpressApp1/ExpressApp1/node_modules/@types/debug/index"' can only be default-imported using the 'esModuleInterop' flag
[更新] 当我暂停 Resharper Ultimate 时警告消失。
如果我切换到 ES5 并尝试
import from 'debug' ( with or without a semicolon )
我看到一个错误
can only be default-imported using the 'esModuleInterop' flag
如果我使用
import * as debug from 'debug'
有用
解决方案
而不是使用
import debug = require('debug');
你应该使用
import * as debug from 'debug' // Import everything from that module
// OR
import debug from 'debug' // Import only the class 'debug' from that module
// Depends on the module
这是 ES6 中的新语法
如果您想使用旧样式的导入模块,您可以在文件中设置"target"
为。"ES5"
tsconfig.json
推荐阅读
- python - 从python代码中获取列表索引超出范围
- apache-kafka - 本地单主机Fabric网络中基于kafka的OSN,但是当我执行事务时没有登录kafka容器
- javascript - 如何使滑块的长度更短并出现在页面的中心?
- json - 如何将循环集成到我的代码中,将我的请求应用于每个 filterViewId?
- python - 为一个接口创建多个 IP,并能够在特定端口上的每个 IP 上运行服务
- linux - Linux 父/子进程组和信号
- arrays - 在 Neo4j 中使用 apoc.load.jason() 时如何拥有一组 Json 键对象?
- symfony - 如何防止实体在容器中被列为服务?
- arrays - 按组复制 SAS 中的值
- c++ - 嵌套别名模板的模板参数推导编译器差异