Javascript require method is not finding axios module

Viewed 1283

I am creating a chrome extension for gmail. I have installed axios using "npm install axios" and that has created a node_modules folder with the module included. I then tried to include the module in my extension.js file but gives "cannot find module 'axios'" error. The axios documentation says "In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with require() use the following approach:

 const axios = require('axios').default;

So i placed it in my extension.js

const axios = require('axios').default;

The axios location leads to the index.d.ts file. I am not sure why it does not go to the index.js where the

module.exports = require('./lib/axios');

is located.

package-lock.json

{
  "requires": true,
  "lockfileVersion": 1,
  "dependencies": {
    "axios": {
      "version": "0.21.1",
      "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
      "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
      "requires": {
        "follow-redirects": "^1.10.0"
      }
    },
    "follow-redirects": {
      "version": "1.13.3",
      "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz",
      "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA=="
    }
  }
}
2 Answers

It's because you used the .default in your request that's for typescript.

Simply use const axios = require('axios');

if it's still not working do npm i again and then npm i axios again.

Related