【问题标题】:Expo and metro bundler: external module cannot resolve react import世博会和地铁捆绑器:外部模块无法解析反应导入
【发布时间】:2021-11-20 01:59:21
【问题描述】:

我使用extraNodeModules 设置配置了metro.config.js,因此metro 会从我的项目外部找到我的共享模块(“monorepo”配置)。

Metro 正在查找我的外部模块(请参阅下面的哑模块),但是当该外部模块尝试导入 react 时出现错误。当导入的模块没有导入反应时,我没有收到错误。

这是错误:

Unable to resolve module @babel/runtime/helpers/interopRequireDefault from /Users/jim/.../modules/dumb-module/index.js: @babel/runtime/helpers/interopRequireDefault could not be found within the project.

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
> 1 | import React from 'react';
  2 | import { Text } from 'react-native';
  3 |
  4 | const DumbModule = () => {

因为它显示的是dumb-module的内容,所以很明显能够找到我的外部模块。但它似乎无法解析该模块尝试导入反应。

正如您所料,我已经尝试了所有方法。很想在这里得到一些想法。

metro.config.js

const path = require('path');

const extraNodeModules = {
    'modules': path.resolve(path.join(__dirname, '../../modules'))
};

const watchFolders = [
    path.resolve(path.join(__dirname, '../../modules'))
];

const nodeModulesPaths = [path.resolve(path.join(__dirname, './node_modules'))];

module.exports = {
    transformer: {
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: true,
                inlineRequires: true,
            },
        }),
    },
    resolver: {
        extraNodeModules,
        nodeModulesPaths
    },
    watchFolders
};

哑模块/index.js

import React from 'react';
import { Text } from 'react-native';

const DumbModule = () => {
    return (
        <Text>I am useless.</Text>
    );
};

export default DumbModule;

展会诊断:

expo diagnostics

  Expo CLI 4.12.0 environment info:
    System:
      OS: macOS 11.6
      Shell: 5.8 - /bin/zsh
    Binaries:
      Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
      Yarn: 1.22.11 - ~/.nvm/versions/node/v14.16.1/bin/yarn
      npm: 7.24.0 - ~/.nvm/versions/node/v14.16.1/bin/npm
    Managers:
      CocoaPods: 1.11.2 - /usr/local/bin/pod
    SDKs:
      iOS SDK:
        Platforms: iOS 15.0, DriverKit 20.4, macOS 11.3, tvOS 15.0, watchOS 8.0
    IDEs:
      Xcode: 13.0/13A233 - /usr/bin/xcodebuild
    npmPackages:
      expo: ~42.0.1 => 42.0.4 
      react: 16.13.1 => 16.13.1 
      react-dom: 16.13.1 => 16.13.1 
      react-native: https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz => 0.63.2 
      react-native-web: ~0.13.12 => 0.13.18 
    npmGlobalPackages:
      expo-cli: 4.12.0
    Expo Workflow: managed

【问题讨论】:

    标签: react-native expo monorepo metro-bundler


    【解决方案1】:

    所以,这实际上工作正常,我的问题是我没有正确重置 Metro 缓存。

    我会留下这篇文章,因为这个 metro.config.js 是让 expo 应用程序在 monorepo 中工作并利用共享组件的解决方案的一部分。

    另一部分是在 expo 应用文件夹中使用 yarn 的 nohoist 选项,因此它的依赖关系保存在其 node_modules 中。

    【讨论】:

    • 我将添加 expo SDK 43 清理了对 monorepos 的支持。现在一切都好。
    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 2014-05-27
    • 2018-05-24
    • 1970-01-01
    • 2022-01-23
    • 2018-10-08
    • 2021-09-13
    • 2017-04-05
    相关资源
    最近更新 更多