简体   繁体   English

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

[英]Metro Bundler Looking for JS files in react-native directory

I am having a weird issue with React native over here.我在这里遇到了 React Native 的一个奇怪问题。 Maybe it is not even RN, but the Metro Bundler.也许它甚至不是 RN,而是 Metro Bundler。

I have literally just created a brand new project using我实际上刚刚使用创建了一个全新的项目

react-native init TestProject react-native init 测试项目

After this, I simply ran在这之后,我只是跑了

react-native run-ios --simulator="iPhone 7" react-native run-ios --simulator="iPhone 7"

Usually when I run-ios, the Metro bundler will, for example, look inside "/Users/fred/Documents/Development/TestProject" - right where my index.js file resides.例如,通常当我运行 ios 时,Metro 打包器会查看“/Users/fred/Documents/Development/TestProject”内部——我的 index.js 文件所在的位置。

However - for some reason now, and on every new project that I create - it now looks at this path:但是 - 现在出于某种原因,在我创建的每个新项目中 - 它现在查看这条路径: 在此处输入图片说明

And it results in this error:它导致此错误: 在此处输入图片说明

I have also tried to change the project root path in the metro.config.js我还尝试更改了metro.config.js的项目根路径

const path = require('path');

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

But it does not seems to work.但它似乎不起作用。

Does anyone know why this is happening, and perhaps how I can fix it?有谁知道为什么会发生这种情况,也许我该如何解决?

Please let me know if I missed anything which I should have posted here.如果我错过了我应该在这里发布的任何内容,请告诉我。

EDIT 1:编辑 1:

I have managed to get it to run in a way that I don't like that much.我设法让它以一种我不太喜欢的方式运行。 Once I have run react-native run-ios , and the console comes up with the incorrect node_modules/react-native JS Metro Bundler console, I close that console, and KEEP the simulator open.一旦我运行了react-native run-ios ,并且控制台出现了错误的node_modules/react-native JS Metro Bundler 控制台,我就会关闭该控制台,并保持模拟器打开。

Then I run this command react-native start --reset-cache , and 'Command + R' the Simulator - which causes the correct path to execute.然后我运行这个命令react-native start --reset-cache和 'Command + R' 模拟器 - 这会导致执行正确的路径。 See these screenshots:请参阅这些屏幕截图:

在此处输入图片说明

Then close the metro bundler console, and run react-native start --reset-cache :然后关闭 Metro bundler 控制台,并运行react-native start --reset-cache

在此处输入图片说明

Here we can see that the bundler now looks at the correct root path:在这里我们可以看到打包器现在查看正确的根路径: 在此处输入图片说明

And then I just Command + R the Simulator:然后我只是Command + R模拟器: 在此处输入图片说明

Now, although I can continue working for now - I mean, WHY is this happening now?现在,虽然我现在可以继续工作 - 我的意思是,为什么现在会发生这种情况? I have always just used the good old react-native run-ios and all was good.我一直只使用旧的react-native run-ios ,一切都很好。 To do things this way is just plain cumbersome and unnecessary.以这种方式做事很麻烦而且没有必要。

Any help will be greatly appreciated!任何帮助将不胜感激!

EDIT 2:编辑2:

I am now here - where I change the root string where it is causing the problem at:我现在在这里 - 我在那里更改了导致问题的根字符串:

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

Inside of the setupAndRun() function.setupAndRun()函数内部。

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

Anyone perhaps know why this line is causing an issue?任何人都可能知道为什么这条线会导致问题?

So - after a month, I realised something - I was doing this within the Visual Studio Code Terminal .所以 - 一个月后,我意识到了一些 - 我was doing this within the Visual Studio Code Terminal I simply switched to my main terminal not within VS Code, and BOOM , it was working as expected.我只是切换到不在 VS Code 和BOOM主终端,它按预期工作。 Tried it within VS Code, and it failed! Found the culprit!找到罪魁祸首了! Nothing related to react native or metro bundler!与本机或地铁捆绑器无关! Damn you VS Code!该死的 VS Code! Still love you though!不过还是爱你!

Quick solution: run in separate console:快速解决方案:在单独的控制台中运行:

npm start -- --reset-cache

2) Or fix bug in "@react-native-community/cli": "1.11.2" using patch-package : 2) 或者使用patch-package修复"@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

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

This sets the path correctly and provides a number of Command Palette commands for using the Packager.这将正确设置路径并提供许多用于使用 Packager 的命令面板命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM