react-native init 初始化项目报错 TypeError: cli.init is not a function

2023-10-16 14:27:51 浏览数 (1)

问题

代码语言:javascript复制
$ react-native init AwesomeProject
...
/usr/local/lib/node_modules/react-native-cli/index.js:302
  cli.init(root, projectName);
      ^

TypeError: cli.init is not a function
    at run (/usr/local/lib/node_modules/react-native-cli/index.js:302:7)
    at createProject (/usr/local/lib/node_modules/react-native-cli/index.js:249:3)
    at init (/usr/local/lib/node_modules/react-native-cli/index.js:200:5)
    at Object.<anonymous> (/usr/local/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

解决办法

使用 npm -g list 查看已安装的 react-native 库

代码语言:javascript复制
$ npm -g list
/usr/local/lib
├── 。。。
├── react-native-cli@2.0.1
├── 。。。

卸载 react-native-cli 库

代码语言:javascript复制
npm uninstall -g react-native-cli
npm uninstall -g react-native  // 如果有,也一起卸载了

使用 npx react-native init 重新初始化项目

代码语言:javascript复制
npx react-native init AwesomeProject

0 人点赞