【问题标题】:React Native: `styled-components/native` cannot be resolved by bundler under yarn workspaceReact Native:`styled-components/native` 无法通过 yarn 工作区下的 bundler 解析
【发布时间】:2020-05-31 11:02:00
【问题描述】:

我最近使用 yarn 工作区和现有的 react native 项目迁移到 monorepo。现在,我在“packages/”下有一个名为“timeylo”的 react native 项目,它的 react-native 依赖项不会被 yarn 提升。

我在打包 JavaScript 时遇到的错误:

error: bundling failed: Error: Unable to resolve module `styled-components/native` from `/Users/sunhe/Documents/Projects/issue/issue-repo/packages/timeylo/src/app.tsx`: Module `styled-components/native` does not exist in the Haste module map or in these directories:
  /Users/sunhe/Documents/Projects/issue/issue-repo/node_modules/styled-components

我有一个用于重放错误的 repo:https://github.com/bidiu/issue-repo

您需要做的就是:

  • yarn 在根目录下,
  • cd packages/timeylo
  • yarn start 启动捆绑器
  • 在另一个终端中,yarn react-native run-ios(或使用 Xcode)。

您应该会在捆绑程序会话下看到错误。

更多信息react-native info如果你需要:)

System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
    Memory: 207.65 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.15.3 - /var/folders/dl/9r8h7nj57ps6h4t4jpwd_1kh0000gn/T/yarn--1581841642552-0.27345581007574116/node
    Yarn: 1.21.1 - /var/folders/dl/9r8h7nj57ps6h4t4jpwd_1kh0000gn/T/yarn--1581841642552-0.27345581007574116/yarn
    npm: 6.10.3 - ~/.nvm/versions/node/v10.15.3/bin/npm
    Watchman: 4.9.0 - /opt/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5791312
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.8.6 => 16.8.6 
    react-native: 0.60.5 => 0.60.5 
  npmGlobalPackages:
    react-native-cli: 2.0.1

非常感谢!!

【问题讨论】:

  • 试试npm i styled-components --save

标签: reactjs react-native yarnpkg styled-components yarn-workspaces


【解决方案1】:

对于纱线:

yarn add styled-component

对于 npm:

npm install --save styled-component

【讨论】:

  • 小心为您的项目或 repo 使用一个包管理器。混合使用 yarn 和 npm 可能会导致已安装包的路径解析问题,并导致类似于您遇到的错误。例如,我用 npm 安装了几个包,但在安装模块时使用 yarn install 以使项目正常工作,然后我不得不删除 yarn.locknode_modules 然后运行 ​​npm install 来修复我遇到的模块解析错误这与您所经历的相似。
【解决方案2】:

我在打开一个新终端并再次尝试解决它时遇到了同样的问题 npm install --save styled-components 也应该修复它。

【讨论】:

    【解决方案3】:
     npm install styled-components --save
    

    试试上面一个,它会起作用的

    【讨论】:

      猜你喜欢
      • 2022-09-25
      • 1970-01-01
      • 2018-03-06
      • 2020-01-08
      • 2017-09-29
      • 2021-05-09
      • 1970-01-01
      • 2018-10-20
      • 2020-10-19
      相关资源
      最近更新 更多