简体   繁体   中英

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.

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...

  1. npm install --save flux-sdk;
  2. import Flux from 'flux-sdk';

Fails on import ( Step 2 ) with the error shown above.

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:

  • Create a 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 :

declare module 'flux-sdk' {
  const flux: any; // you can define whatever is now exported
  export default flux;
}

  • Then just include typings in your tsconfig.json :
{
  "compilerOptions": {
    // ...
  },
  "include": [
    "typings"
  ],
}

Kindly try these solutions.

Method 1: Simply use require instead of 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

"noImplicitAny": false,
"allowJs": true,

I hope they would help you, thanks.

Maybe you use old package-lock.json. Please delete package-lock.json&node_modules and reinstall node modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM