Unable to resolve module some-module from /Users/username/projectname/AwesomeProject/index.js: Invalid directory /Users/node_modules/some-module
在react-native運行的時候發現這個錯誤。起因是在項目中添加了js模塊并引用了它。
require('newmodule');
發現node_modules路徑出錯。按照提示的方法去修復它,對我沒用。
$watchman watch-del-all
$ rm -rf node_modules && npm install
$ rm -fr $TMPDIR/react-*
于是想到我下載過react-native 的源代碼,并且在里面運行過npm install. 會不會因此沖突。果斷卸載全局node_modules重裝。
$ npm uninstall -g react-native
$ npm install -g react-native-cli
你會發現在/usr/local/bin/下面有個react-native的link, 會連接到/usr/local/lib/node_modules. react-native變成全局的了。接下來,刪掉你的項目重新初始化。
$ rm -rf AwesomeProject
$ react-native init AwsomeProject
react-native init會重新下載項目所依賴的node_modules. 然后添加你的module. 成功運行。
react-native源碼里面的examples沒有node_modules. 估計是配置到了上層目錄的node_modules. 具體怎么配置的還要研究一下。