크로스 플랫폼/React-Native <JSX>

[React-Native] cli.init is not a function 문제 해결하기

TaeGyeong Lee 2023. 7. 20. 16:43

발생 에러

/opt/homebrew/Cellar/react-native-cli/2.0.1/libexec/lib/node_modules/react-native-cli/index.js:302
  cli.init(root, projectName);
      ^

TypeError: cli.init is not a function
    at run (/opt/homebrew/Cellar/react-native-cli/2.0.1/libexec/lib/node_modules/react-native-cli/index.js:302:7)
    at createProject (/opt/homebrew/Cellar/react-native-cli/2.0.1/libexec/lib/node_modules/react-native-cli/index.js:249:3)
    at init (/opt/homebrew/Cellar/react-native-cli/2.0.1/libexec/lib/node_modules/react-native-cli/index.js:200:5)
    at Object.<anonymous> (/opt/homebrew/Cellar/react-native-cli/2.0.1/libexec/lib/node_modules/react-native-cli/index.js:153:7)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

 

해결 방법

해당 문제는 homebrew를 통해 react-native-cli를 설치한 환경에서 리넥 프로젝트를 생성/빌드할 때 발생할 수 있는 문제입니다. 먼저  brew를 통해 react-native-cli가 설치되었는 지 확인해 주세요.

brew를 통해 react-native-cli를 제거해 주세요.

brew uninstall react-native-cli

npm 을 통해 @react-native-community/cli를 전역적으로 설치해 주세요.

npm install -g @react-native-community/cli

옵션) 구동하려는 react-native 프로젝트가 구버전 프로젝트인 경우 이 글을 통해 호환되는 버전을 설치해 주세요.

 

왜?

호환성 문제입니다. homebrew의 react-native-cli의 최신 버전은 2.0.x로 현재 react-native 프로젝트를 지원하지 않는 라이브러리입니다.