프론트엔드 개발

[next js + typescript 모듈 import error]Could not find a declaration file for module 본문

Front-End/Error 해결

[next js + typescript 모듈 import error]Could not find a declaration file for module

태나미 2021. 8. 14. 19:34
Could not find a declaration file for module 'superagent-promise'. 'C:/Users/taenam.DESKTOP-VMNKLBD/Desktop/managent/my-app/node_modules/superagent-promise/index.js' implicitly has an 'any' type. Try `npm i --save-dev @types/superagent-promise` if it exists or add a new declaration (.d.ts) file containing `declare module 'superagent-promise';`

next js로 만든 프로젝트를 빌드할 때, 위와 같은 에러가 나왔는데, npm으로는 추가가 안되어서 new declaration (.d.ts) file 방법으로 해결한걸 공유드릴게요~

 

아래에는 해결방법을 순서대로 적어보겠습니다.

저는 예시로 superagent-promise 모듈을 적용시키겠습니다.

1. d.ts 파일 생성

1. d.ts 파일 생성

생성위치 [root]/@types/[모듈명]/index.d.ts 생성

예시) MY-APP/@types/superagent-promise/index.d.ts

2. index.d.ts 파일안에 모듈 내용 넣기

index.d.ts안에 아래에 내용을 넣습니다

declare module [모듈명]

예시는 아래와 같습니다

declare module 'superagent-promise';

3. tsconfig.json 설정

{
  "compilerOptions": {
    "typeRoots" : ["./@types", "./node_modules/@types"]
  },
}

 

 

출처: https://kimchanjung.github.io/programming/2020/07/05/typescipt-import-js-module-error/

Comments