Metro Bundler在react-native目录中寻找JS文件

时间:2019-03-23 18:25:12

标签: react-native

在这里,我与React native有一个奇怪的问题。也许甚至不是RN,而是Metro Bundler。

我刚刚使用

创建了一个全新的项目
  

本机初始化TestProject

在此之后,我就跑了

  

react-native run-ios --simulator =“ iPhone 7”

通常,当我运行ios时,Metro捆绑程序将在“ / Users / fred / Documents / Development / TestProject”内部-我的index.js文件所在的位置。

但是-由于某种原因,并且现在在我创建的每个新项目中-现在都会查看以下路径: enter image description here

它导致此错误: enter image description here

我还尝试过更改metro.config.js中的项目根路径

const path = require('path');

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  projectRoot: path.resolve(__dirname),
};

但这似乎不起作用。

有人知道为什么会这样吗,也许我该如何解决?

请让我知道是否错过了应该在此处发布的内容。

编辑1:

我设法使它以我不太喜欢的方式运行。 运行react-native run-ios后,控制台出现了不正确的node_modules/react-native JS Metro Bundler控制台,我关闭该控制台,并保持模拟器打开。

然后我运行此命令react-native start --reset-cache,并运行“ Command + R”模拟器-这将导致执行正确的路径。请参阅以下屏幕截图:

enter image description here

然后关闭Metro bundler控制台,并运行react-native start --reset-cache

enter image description here

在这里我们可以看到捆绑器现在查看正确的根路径: enter image description here

然后我只是Command + R模拟器: enter image description here

现在,尽管我现在可以继续工作-我的意思是,为什么现在会发生这种情况?我一直只使用旧的react-native run-ios,一切都很好。用这种方式做事只是繁琐而不必要的。

任何帮助将不胜感激!

编辑2:

我现在在这里-在以下位置更改引起问题的根字符串:

node_modules/@react-native-community/cli/build/cliEntry.js

setupAndRun()函数内部。

  

const root = options.projectRoot吗? _path()。default.resolve(options.projectRoot):process.cwd();

也许有人知道为什么这条线会引起问题吗?

3 个答案:

答案 0 :(得分:0)

-一个月后,我意识到了-)。我只是切换到不在VS Code内的主终端,was doing this within the Visual Studio Code Terminal可以正常工作。 BOOM找到了罪魁祸首!与本机或Metro Bundle无关!该死的VS Code!仍然爱你!

答案 1 :(得分:0)

快速解决方案:在单独的控制台中运行:

npm start -- --reset-cache

2)或使用"@react-native-community/cli": "1.11.2"修复patch-package中的错误:

diff --git a/node_modules/@react-native-community/cli/build/cliEntry.js b/node_modules/@react-native-community/cli/build/cliEntry.js
index 7fa03a8..171e019 100644
--- a/node_modules/@react-native-community/cli/build/cliEntry.js
+++ b/node_modules/@react-native-community/cli/build/cliEntry.js
@@ -198,7 +198,15 @@ async function setupAndRun() {


   const options = (0, _minimist().default)(process.argv.slice(2));
-  const root = options.projectRoot ? _path().default.resolve(options.projectRoot) : process.cwd();
+
+  var path = require('path')
+  var root
+  // Fix bug caused by removed projectRoot: https://github.com/react-native-community/cli/issues/484#issuecomment-508787630
+  if (process.argv[1].endsWith('node_modules/react-native/cli.js') && options['_'][0] == 'start')
+    root = path.normalize(process.cwd() + '/../..')
+  else
+    root = options.projectRoot ? _path().default.resolve(options.projectRoot) : (process.cwd() );
+
   const reactNativePath = options.reactNativePath ? _path().default.resolve(options.reactNativePath) : (() => {
     try {
       return _path().default.dirname( // $FlowIssue: Wrong `require.resolve` type definition
diff --git a/node_modules/@react-native-community/cli/build/commands/server/external/xsel b/node_modules/@react-native-community/cli/build/commands/server/external/xsel
old mode 100644
new mode 100755

答案 2 :(得分:0)

如果您使用的是 Visual Studio Code,请安装:https://marketplace.visualstudio.com/items?itemName=msjsdiag.vscode-react-native

这会正确设置路径并提供许多用于使用打包程序的命令面板命令。