首页 > 解决方案 > 如何为第三方包做模块名称别名

问题描述

我有一个由react-native init命令创建的应用程序。我的应用程序导入 websocket 包,而该包又需要 http 包并导致错误提示“无法解析模块 http”。

即:myApp -> 3rd-module -> ws -> http

我尝试通过安装“@tradle/react-native-http”来解决问题,并将以下行添加到我的应用程序包 json 文件中:

"browser": { "http": "@tradle/react-native-http" }, 
"react-native": { "http": "@tradle/react-native-http" },

但它不起作用。

我也尝试使用 babel-plugin-module-resolver 但也不走运。这是我的 .babelrc :

{
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    ["module-resolver", {
      "alias": {
        "@tradle/react-native-http": "http"
      }
    }]
  ]
}

如何为我的案例做别名?我研究通过使用 webpack 配置来解决这个问题,但不知道配置文件在哪里。在 google 之后,我认为react-native init创建的项目使用 Metro config 而不是 webpack。

标签: react-native

解决方案


尝试

    ["module-resolver", {
      "alias": {
        "http":"@tradle/react-native-http"
      }
    }]

推荐阅读