'Cannot find module 'axios'

I am coding a small program for Firebase Functions, using node.js. I always install my node modules using npm and i also installed axios using npm only.

However when I do firebase deploy I get this error:

enter image description here

This is my package-lock.json

{
  "requires": true,
  "lockfileVersion": 1,
  "dependencies": {
    "axios": {
      "version": "0.19.0",
      "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz",
      "integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==",
      "requires": {
        "follow-redirects": "1.5.10",
        "is-buffer": "^2.0.2"
      }
    },
    "debug": {
      "version": "3.1.0",
      "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
      "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
      "requires": {
        "ms": "2.0.0"
      }
    },
    "follow-redirects": {
      "version": "1.5.10",
      "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
      "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
      "requires": {
        "debug": "=3.1.0"
      }
    },
    "is-buffer": {
      "version": "2.0.4",
      "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz",
      "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A=="
    },
    "ms": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
      "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
    }
  }
}

When I remove const axios = require('axios'); from my index.js, the rest of my code compiles and works just fine.

Any idea what is going on?

Thank you!



Solution 1:[1]

Looks like you are missing axios dependency in your package.json file.

Go to the functions directory and install axios. This will declare the axios dependency in the package.json for you. It works for me.

npm install axios --save

Redeploy your Cloud Function for Firebase using:

firebase deploy

Solution 2:[2]

Command prompt then go to your folder and npm install axios. Finally, run the code. It worked for me

Solution 3:[3]

My answer may not fix your issue, but might help others. I have received an error message "Uncaught Error Error: Cannot find module 'axios'" then I tried to install axios package by running the command npm install axios which fixed the problem.

  • Note: I had package.json empty (not even a pair of curly braces). So running the nmp command was failing with error

    npm install axios
    npm ERR! code EJSONPARSE
    So I added a pair of curly braces {} to my package.json and executed the same command again fixed the issue.

    npm install axios
    added 2 packages, and audited 3 packages in 1s
    1 package is looking for funding
    run npm fund for details
    found 0 vulnerabilities

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sllopis
Solution 2 Huy Sam
Solution 3 shary.sharath