breeze wang
breeze wang

Reputation: 237

How to do module name alias for third party package

I have a app that created by react-native init command. My app import websocket package which in turn require http package and cause error said "Unable to resolve module http".

i.e: myApp -> 3rd-module -> ws -> http

I try to work-around by install "@tradle/react-native-http", and added follow lines to my app's package json file:

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

but it doesn't work.

I also try using babel-plugin-module-resolver but unluck either. Here is my .babelrc :

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

How to do alias for my case? I research to fixing this problem by using webpack configuration, but don't know where is the configure file. After google, i think project created by react-native init use metro config instead of webpack.

Upvotes: 2

Views: 1137

Answers (1)

gavan
gavan

Reputation: 21

try

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

Upvotes: 2

Related Questions