我按照以下链接开始使用 react-native
我尝试在没有博览会的情况下创建本机应用程序,因此我按照文档遵循以下命令
npm install -g react-native-cli
react-native init AwesomeProject
运行android命令后
react-native run-android
它给了我以下模拟器错误
所以我使用 start 命令来运行 Metro 服务器
react-native start
此命令在控制台中给了我另一个错误
我按照以下链接开始使用 react-native
我尝试在没有博览会的情况下创建本机应用程序,因此我按照文档遵循以下命令
npm install -g react-native-cli
react-native init AwesomeProject
运行android命令后
react-native run-android
它给了我以下模拟器错误
所以我使用 start 命令来运行 Metro 服务器
react-native start
此命令在控制台中给了我另一个错误
使用某些 NPM 和 Node 版本的 Metro 存在问题。
你有两种选择:
\node_modules\metro-config\src\defaults\blacklist.js并更改以下代码:var sharedBlacklist = [
/node_modules[/\\]react[/\\]dist[/\\].*/,
/website\/node_modules\/.*/,
/heapCapture\/bundle\.js/,
/.*\/__tests__\/.*/
];
对此:
var sharedBlacklist = [
/node_modules[\/\\]react[\/\\]dist[\/\\].*/,
/website\/node_modules\/.*/,
/heapCapture\/bundle\.js/,
/.*\/__tests__\/.*/
];
请注意,如果您运行
npm install或yarn install您需要再次更改代码。
它的 Nodejs 兼容性问题我卸载了我的 Node(12.11) 并安装了 Node(10) 稳定版本,它运行良好。
解决方案是更改blacklist.jsmodulemetro-config文件中的文件,如上所述。但是每次运行时,npm/yarn install您都必须再次更改它。
所以我想出了一个解决方案,可以节省您每次访问文件和更改文件的时间:
我使用了一个使用 JavaScript 编辑文件的库:
npm install --save replace-in-file
创建一个与node_module文件夹名称相同级别的文件:metro-fix.js每个示例。
将此脚本复制粘贴到其中:
//Load the library
const replace = require('replace-in-file');
// path for metro config file
const path = 'node_modules/metro-config/src/defaults/blacklist.js';
// creating options for editing the file
const options = [
{
files: path,
from: 'modules[/',
to: 'modules[\\/',
},
{
files: path,
from: 'react[/',
to: 'react[\\/',
},
{
files: path,
from: 'dist[/',
to: 'dist[\\/',
},
];
try {
let results;
// looping on each option
options.forEach(e => {
results = replace.sync(e);
console.log('Replacing "'+e.from+'" by "'+e.to+'" results:', results[0].hasChanged);
});
} catch (error) {
console.error('Error occurred:', error);
}
npm install只需运行:node metro-fix.js
请参阅替换文件文档。