[英]Can't find module or its corresponding type declarations when trying to import SDK in Angular Project
Currently attempting to use an SDK in an existing Angular project and we are unable to find the cause to the following problem.当前尝试在现有的 Angular 项目中使用 SDK,我们无法找到以下问题的原因。
Here is an image of the error:这是错误的图像:
Cannot find module 'flux-sdk' or its corresponding type declarations
After doing some digging we found various post with the same issue and have tried just about everything we could find and are still unable to solve the error.在进行了一些挖掘之后,我们发现了具有相同问题的各种帖子,并且几乎尝试了我们能找到的所有内容,但仍然无法解决错误。
Steps...脚步...
npm install --save flux-sdk;
import Flux from 'flux-sdk';
Fails on import
( Step 2 ) with the error shown above. import
失败(步骤 2),出现上述错误。
We are not sure why this is happening and because we have tried the other solutions found online and unable to solve the issue I come here.我们不确定为什么会发生这种情况,因为我们已经尝试了在网上找到的其他解决方案,但无法解决我来这里的问题。
I think you could achieve adding typing for a package which doesn't have type definition like this package flux-sdk
in an Angular project by following steps:我认为您可以通过以下步骤为一个没有类型定义的包添加类型,例如 Angular 项目中的这个包
flux-sdk
:
typings
directory at the root level then create the typing for this package which is likely called flux-sdk.d.ts
:typings
目录,然后为这个包创建类型,它可能被称为flux-sdk.d.ts
: typings/flux-sdk.d.ts
: typings/flux-sdk.d.ts
:
declare module 'flux-sdk' {
const flux: any; // you can define whatever is now exported
export default flux;
}
typings
in your tsconfig.json
:typings
在tsconfig.json
:{
"compilerOptions": {
// ...
},
"include": [
"typings"
],
}
Kindly try these solutions.请尝试这些解决方案。
Method 1: Simply use require
instead of import
.方法 1:只需使用
require
而不是import
。
// import { Flux } from 'flux-sdk'
const Flux = require('flux-sdk');
Method 2: Try to replace these line in the tsconfig.json
if you want use import
方法 2:如果要使用
import
请尝试替换tsconfig.json
这些行
"noImplicitAny": false,
"allowJs": true,
I hope they would help you, thanks.我希望他们能帮助你,谢谢。
Maybe you use old package-lock.json.也许您使用旧的 package-lock.json。 Please delete package-lock.json&node_modules and reinstall node modules.
请删除 package-lock.json&node_modules 并重新安装节点模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.