react-native package specifices a main module https module

Viewed 2466

I made a test project, only trying to include https. I used expo init test to initialize the project, then npm install https to install the library.

I then edited App.js and included this line at the very top: import {https} from 'https';

Finally expo start to start the expo server.

Now I get the following error:

InternalError Metro has encountered an error: While trying to resolve module `https` from file `/path/test/App.js`, the package `/path/test/node_modules/https/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/path/test/node_modules/https/index.js`. Indeed, none of these files exist:

  * /path/test/node_modules/https/index.js(.native|.android.expo.ts|.native.expo.ts|.expo.ts|.android.expo.tsx|.native.expo.tsx|.expo.tsx|.android.expo.js|.native.expo.js|.expo.js|.android.expo.jsx|.native.expo.jsx|.expo.jsx|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.wasm|.native.wasm|.wasm)
  * /path/test/node_modules/https/index.js/index(.native|.android.expo.ts|.native.expo.ts|.expo.ts|.android.expo.tsx|.native.expo.tsx|.expo.tsx|.android.expo.js|.native.expo.js|.expo.js|.android.expo.jsx|.native.expo.jsx|.expo.jsx|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.wasm|.native.wasm|.wasm)

I've verified that these files do indeed not exist, here is the output of ls node_modules/https: package.json

So I'm guessing I did something wrong during the install process, however, I can't figure out what. (I've also tried several times to install using yarn add https, I got the same error)

2 Answers

The https module you want is almost certainly the Node.js built-in https module and is definitely not the six-year-old https npm module with no documentation or code and merely a lone package.json file. That package should probably be deprecated so people don't make this entirely understandable mistake.

In short, try npm uninstall https and see if your code starts working. It probably will.

If not and you really need some external module, it's definitely not that module. Look at one of the many many great http/https modules out there. @Nitish suggests axios in their answer and that is a popular and solid choice.

import {Agent} from 'https'; should work with the built-in https module assuming you're working with a reasonably recent version of Node.js. (You can quickly test your version of Node.js for this by putting that line of code in an index.mjs file and running node index.mjs. If there's no error message, it worked.)

Disclaimer: I know nothing about expo. This is all from a Node.js/npm perspective.

the error is clear that your package.json don't have https module. I don't know why you need this module but if you need this for REST API I suggest fetch or if third party you can use axios.

Related