react-native开始给出无效的正则表达式无效的错误

时间:2019-09-26 13:00:48

标签: reactjs react-native react-native-android react-native-cli metro-bundler

我按照下面的链接开始使用react-native

Getting started with react native

我尝试创建没有博览会的本机应用程序 所以我按照文档遵循以下命令

- (id)forwardingTargetForSelector:(SEL)aSelector{
    if(MHFProtocolHasInstanceMethod(@protocol(UITableViewDelegate), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return self.fetchedTableViewController;
        }
    }
    else if(MHFProtocolHasInstanceMethod(@protocol(UITableViewDataSource), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return self.fetchedTableViewController;
        }
    }
    else if(MHFProtocolHasInstanceMethod(@protocol(NSFetchedResultsControllerDelegate), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return self.fetchedTableViewController;
        }
    }
    else if(MHFProtocolHasInstanceMethod(@protocol(UIDataSourceModelAssociation), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return self.fetchedTableViewController;
        }
    }
    return [super forwardingTargetForSelector:aSelector];
}

- (BOOL)respondsToSelector:(SEL)aSelector{
    if([super respondsToSelector:aSelector]){
        return YES;
    }
    else if(MHFProtocolHasInstanceMethod(@protocol(UITableViewDelegate), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return YES;
        }
    }
    else if(MHFProtocolHasInstanceMethod(@protocol(UITableViewDataSource), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return YES;
        }
    }
    else if(MHFProtocolHasInstanceMethod(@protocol(NSFetchedResultsControllerDelegate), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return YES;
        }
    }
    else if(MHFProtocolHasInstanceMethod(@protocol(UIDataSourceModelAssociation), aSelector)){
        if([self.fetchedTableViewController respondsToSelector:aSelector]){
            return YES;
        }
    }
    return NO;
}

运行android命令后

npm install -g react-native-cli
react-native init AwesomeProject

它在模拟器上给了我以下错误

enter image description here

所以我用启动命令来运行Metro服务器

react-native run-android

此命令在控制台中给了我另一个错误

enter image description here

10 个答案:

答案 0 :(得分:8)

它与Node.js的兼容性问题 我卸载了Node(12.11)并安装了Node(10)稳定版本,并且运行良好。

答案 1 :(得分:6)

使用某些NPM和Node版本的Metro出现问题。

您备有2种选择:

  • 替代1:卸载节点和npm,然后重新安装其他版本。
  • 替代2:热门文件\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安装或yarn安装,则需要再次更改代码。

答案 2 :(得分:2)

有人问了一个类似的问题here,也许该解决方案可能对您有效,因为我申请并成功了

答案 3 :(得分:1)

当使用choco命令( choco install -y nodejs.install python2 jdk8 )安装NodeJS时,它将在撰写本文时安装最新版本的node 12.12.0。回答。您必须将其降级到10.16.3,然后 运行react-native run-android 命令,它应该可以正常工作

答案 4 :(得分:1)

是的,只需切换到Node版本10,它将可以使用。

nvm install <version>
nvm use <version>

有效...:)

答案 5 :(得分:1)

解决方案是更改模块blacklist.js文件中的metro-config文件,如上所述。但是每次您运行npm/yarn install时,都必须再次进行更改。

所以我想出了一个解决方案,可以节省您前往文件并每次更改的时间:

我使用了一个使用JavaScript编辑文件的库:

  1. 安装文件替换模块:
npm install --save replace-in-file
  1. 创建与node_module文件夹相同级别的文件,并将其命名为:每个示例metro-fix.js

  2. 复制将此脚本粘贴到其中:

//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);
}
  1. 现在,每次您运行npm install时,只需运行:
node metro-fix.js

请参阅Replace-in-file文档。

答案 6 :(得分:0)

close命令提示符和Metro bundler,再次执行

答案 7 :(得分:0)

我通过以下步骤解决了这个问题:

  1. 卸载节点v12并安装节点v8.9.4
  2. 重新安装react-native-cli
  3. 重新创建项目react-native init myApp --version 0.60.0
  4. react-native start

希望有帮助

答案 8 :(得分:0)

是的,我也面临这个问题。实际上,建议大多数用户使用的Node.js是10.16.3 LTS版本。 使用下面的choco命令将安装最新版本的12.11.0。 choco install -y nodejs.install python2 jdk8

我将Node版本从12.11.0降级到10.16.3 LTS,并且对我有用。

答案 9 :(得分:0)

这是因为节点版本更高。

  1. 解决方案之一是,您可以轻松地将节点版本从12.x降级为10.x或卸载12.x并安装10.x

  2. 另一方面,如果您要将12.x用于其他项目,该怎么办。 因此,以下解决方案使其变得更容易。您可以使用nvm来控制节点的多个版本。

因此,首先从这里https://github.com/coreybutler/nvm-windows/releases

安装nvm

安装后,请记住将nvm路径添加到系统变量

然后nvm install 10.18.1 64

然后nvm install 12.14.1 64

列出所有已安装的版本nvm list

无论何时要从一个版本切换到另一版本,请使用命令

nvm use 10.18.1  OR nvm use 12.14.1
相关问题