[React-Native] Cannot find module 'react-native'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option? 에러

nana·2024년 5월 20일
0

React-Native

목록 보기
2/3

오류 내용

Cannot find module 'react-native'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

vsCode에서 React-Native 프로젝트를 실행하려는데 이러한 에러가 자꾸 발생하였다.

이 오류는 주로 TypeScript 설정이나 모듈 경로 문제로 발생한다고 한다.


해결 방법

1. node_modules 폴더 및 package.json 확인 해보기

프로젝트 루트 디렉터리에 node_modules 폴더가 존재하고, react-native 모듈이 설치되어 있는지 확인한다.
node_modules 폴더가 없거나 react-native 모듈이 없다면 다음 명령어로 설치해준다.

npm install react-native

2. TypeScript 설정 (tsconfig.json) 수정 해보기

tsconfig.json 파일을 열고, moduleResolution 옵션을 node로 설정한다.
이 설정은 TypeScript가 모듈을 해석할 때, Node.js 모듈 해석 방식을 사용하도록 해준다.

{
  "compilerOptions": {
    "moduleResolution": "node",
    "paths": {
      "*": ["node_modules/*"]
    },
    ...
  },
  ...
}

3. 경로 설정 확인

TypeScript가 react-native 모듈을 찾지 못하는 경우, paths 옵션을 설정하여 모듈 경로를 명시적으로 지정할 수 있다.

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "react-native": ["node_modules/react-native"]
    },
    ...
  },
  ...
}

나의 경우에 2번으로 해결되었다. 모듈 설정을 안해주어서 오류가 발생했나보다.

profile
프론트엔드 개발자 도전기

0개의 댓글