在打字稿中用 import 替换 require

问题描述 投票:0回答:1

我正在用 NodeJS 开发一个应用程序,需要使用旧包。 问题是在他们的说明中建议的用法是

require
:

var xxx = require("xxx");

但我收到 ELSint 错误

Do not use "require".

如果我简单地将

require
替换为
import
我得到:

TS7016: Could not find a declaration file for module xxx.

这个问题的正确解决方案是什么?

node.js typescript
1个回答
0
投票

您可以使用动态导入

import { createRequire } from 'module';
const require = createRequire(import.meta.url); // Why? relative to absolute path within a module (Browser / server)

const xxx = require('xxx');

顺便说一句,你尝试过延迟加载吗?

await import('xxx')

© www.soinside.com 2019 - 2024. All rights reserved.