之前本来React-Native项目用的好好的,结果手贱更新了一下React-native版本,然后各种运行不了。不过本来就是刚开始的DEMO,所以打算重新新建一个项目React-native init {project} 来重新运行。
可是再也不能生成项目了,一直报
module.js:339
throw err;
^
Error: Cannot find module 'invariant'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/hoyn/Documents/WorkPlace/test1/node_modules/react-native/packager/react-packager/src/node-haste/Module.js:18:19)
at Module._compile (module.js:435:26)
at loader (/Users/hoyn/Documents/WorkPlace/test1/node_modules/react-native/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/hoyn/Documents/WorkPlace/test1/node_modules/react-native/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
在各种试验+无限回滚版本之后发现,并没什么卵用。于是打算卸掉npm,node,nvm之后重装。
如何卸载大家可以在百度查找,我是MAC可能会有些不一样。
卸载干净之后,安装nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
我在重装的时候恰好遇到nvm服务器出现问题,我还以为是我自己的问题,安装了一个下午,后面放弃了,第二天早上来花了1分钟就装好了。如果有遇到这种情况,还是去网上下载一个离线的手动安装吧。
然后安装node
npm install -g v4.2.3
再接着装react-native-cli
如果没有安装cnpm,记得先安装cnpm
sudo npm install cnpm -g
有安装的话跳过上一步,直接
cnpm install -g react-native-cli@0.1.10 -g
我就是傻傻分不清楚react-native-cli和react-native,之前更新了一下react-native导致工程整体不能用了。
装好之后,此时react-native init {project}依然报这个错误。
查了一下Github 和 Stack Overflow ,上面说的是需要更新npm 到3以上 , 此时我的npm版本是2.14.7,这个我猜测跟react-native-cli的版本有关,我这个版本是不需要的。
在更新npm之前我记得有一个n模块可以更新,所以我继续装n模块
npm install -g n
在更新npm模块之前我最后尝试了一次生成react-native 项目。
奇迹发生了 ,居然让我跑通了。不我知道n模块是不是关键,但是可能会解决一些问题,这也算是react-native的爬坑之路吧。
最后附上爬坑之路:
➜ WorkPlace react-native init test1
This will walk you through creating a new React Native project in /Users/hoyn/Documents/WorkPlace/test1
Installing react-native package from npm...
module.js:339
throw err;
^
Error: Cannot find module 'invariant'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/hoyn/Documents/WorkPlace/test1/node_modules/react-native/packager/react-packager/src/node-haste/Module.js:18:19)
at Module._compile (module.js:435:26)
at loader (/Users/hoyn/Documents/WorkPlace/test1/node_modules/react-native/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/hoyn/Documents/WorkPlace/test1/node_modules/react-native/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
➜ WorkPlace npm install invariant warning --save
warning@3.0.0 node_modules/warning
└── loose-envify@1.3.0 (js-tokens@2.0.0)
invariant@2.2.2 node_modules/invariant
└── loose-envify@1.3.0 (js-tokens@2.0.0)
➜ WorkPlace node -v
v4.2.3
➜ WorkPlace npm -v
2.14.7
➜ WorkPlace n v3
zsh: command not found: n
➜ WorkPlace n
zsh: command not found: n
➜ WorkPlace npm install -g n
/Users/hoyn/.nvm/versions/node/v4.2.3/bin/n -> /Users/hoyn/.nvm/versions/node/v4.2.3/lib/node_modules/n/bin/n
n@2.1.4 /Users/hoyn/.nvm/versions/node/v4.2.3/lib/node_modules/n
➜ WorkPlace react-native init test2
This will walk you through creating a new React Native project in /Users/hoyn/Documents/WorkPlace/test2
Installing react-native package from npm...
Setting up new React Native app in /Users/hoyn/Documents/WorkPlace/test2
Installing React...
Installing Jest...
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated lodash.assign@4.2.0: This package is deprecated. Use Object.assign.
To run your app on iOS:
react-native run-ios
- or -
Open ios/test2.xcodeproj in Xcode
Hit the Run button
To run your app on Android:
Have an Android emulator running (quickest way to get started), or a device connected
react-native run-android