【问题标题】:Missing dependencies when using yarn workspace with React native init使用带有 React 本机 init 的 yarn 工作区时缺少依赖项
【发布时间】:2020-08-08 11:27:47
【问题描述】:

我想设置一个 monorepo。 我使用npx react-native init myProject 作为第一个项目来初始化我的 React 本机项目。 (以后会有更多项目添加)

文件夹结构

  • 父母
    • 我的项目
      • package.json(由 react-native 创建)
    • yarn.lock
    • package.json(我设置工作区的地方)

然后我从 myProject 的父文件夹设置 yarn 工作区。

    {   "name": "Parent",   
        "private": true,   
        "workspaces": {
        "packages": [
          "*"
        ],
        "nohoist": [
          "**/react-native",
          "**/react-native-*",
          "**/@react-native-*",
          "**/@react-native-*/**",
          "**/@react-navigation",
          "**/@react-navigation/**",
          "**/hermes-engine",
          "**/rn-*"
        ]   }
     }

在我推送到 git 并克隆回来之前,一切似乎都有效。我使用yarn install,但在启动项目时出现此错误(运行android或运行ios)

Error: Unable to resolve module `scheduler` from `node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js`: scheduler could not be found within the project or in these directories:
  ..\node_modules

我可以解决这个问题的唯一方法是 cd myProject 并运行 npm install(它会添加一些包并且应用程序将运行)而 cd 和使用 yarn install 不会做任何事情

我只是想在项目中使用yarn,我该怎么做才能解决这个问题?

【问题讨论】:

    标签: react-native yarnpkg monorepo yarn-workspaces react-native-cli


    【解决方案1】:

    我知道如何解决它。我忘了编辑 RN 文件夹中的 metro.config。

    const path = require('path')
    
    const linkedLibs = [path.resolve(__dirname, '..')]
    console.info('CONFIG', linkedLibs) 
     
    module.exports = {
      transformer: {
        getTransformOptions: async () => ({
          transform: {
            experimentalImportSupport: false,
            inlineRequires: false,
          },
        }),
      },
      watchFolders: linkedLibs,
    };
    

    使用它,一切都会好起来的

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 2020-06-07
      • 2021-06-23
      • 2021-08-19
      • 2020-03-22
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多