[Typescript] 모듈 설치하기

마리 Mari·2021년 6월 17일
2

문제

이전에 React로 개발했을 때 처럼 모듈을 설치했는데, 모듈을 찾을 수 없다고 나왔다.

$  npm install react-router-dom
Could not find a declaration file for module 'react-router-dom'.
'/Users/***/project-folder/node_modules/react-router-dom/index.js' implicitly has an 'any' type.

Try `npm i --save-dev @types/react-router-dom` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-router-dom';


해결: Type 설치

구글에 typescript react-router-dom으로 검색하였다.

지금 보니 error 메세지에 @types/...를 설치하라는 안내가 있었는데, 이걸 안읽고 검색부터 하다니..
다음부터는 에러메세지도 꼼꼼히 읽어야겠다.

npm install react-router-dom @types/react-router-dom


또 다시 문제

이제 react-router-dom 모듈은 잘 찾는데, 갑자기 react 모듈을 못찾기 시작했다. 모든 component 파일에 에러가 나서 원인을 알기 전까지 무척 당황스러웠다.

이번에는 그냥 error 메세지로 구글에 검색하였다.
Could not find a declaration file for module 'react'.

npm install --save @types/react @types/react-dom

reactreact-dom의 타입도 설치해주었다.



@types ?

앞서 입력한 @types의 의미를 정확히 알고 싶어서,
구글에 npm @types로 검색하였다.

@types/moduleName은 타입선언만 포함하는 모듈이라고 한다.

앞으로 typescript로 개발시 모듈을 추가할 때, 타입 모듈도 함께 설치하는 것도 잊지 말아야겠다.

profile
우리 블로그 정상영업합니다.

0개의 댓글