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

问题描述 投票:1回答:1

我在这里遇到了一个奇怪的问题。也许它甚至不是RN,而是Metro Bundler。

我确实刚刚创建了一个全新的项目

react-native init 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捆绑控制台,并运行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();

有谁知道这条线为什么会引起问题?

react-native
1个回答
0
投票

所以 - 一个月之后,我意识到了一些事情 - 我was doing this within the Visual Studio Code Terminal。我只是切换到我的主终端,而不是在VS Code和BOOM中,它按预期工作。 Tried it within VS Code, and it failed!找到了罪魁祸首!与本机或地铁捆绑器无关!该死的VS代码!尽管如此,仍然爱你!

© www.soinside.com 2019 - 2024. All rights reserved.