Home > Software engineering >  Unable to install Axios to Node -- Tried several StackOverflow solutions to no avail
Unable to install Axios to Node -- Tried several StackOverflow solutions to no avail

Time:10-13

Node is unable to find the axios package I installed. In NPM I tried the following (thinking it was an npm issue):

npm uninstall axios
npm cache clean --force
npm install axios
npm uninstall axios
npm cache clean --force
npm install axios -S

in my app.js file I have the following listed as axios states to do with node:

const axios = require('axios');

I also tried the following:

const axios = require('axios').default;  //I figured this would not work because I am not using typescript
var axios = require('axios');

When I run my local server in npm I get this error:

(function (exports, require, module, __filename, __dirname) { import axios from './lib/axios.js';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Code\WebDevelopment\Newsletter-Signup\app.js:4:15)

I am running npm v5.0.0 and node v8.0.0 I have checked several stackoverflow pages for the solution but none of them seemed to offer a solution to this issue. Here are the pages I have checked: import axios from axios - Parsing error: Unexpected token Unable to load Axios in Node even though it is installed Why do I get an error while trying to install axios? Module not found when new dependency is installed Javascript require method is not finding axios module

I also looked at the GitHub page and couldn't find a solution: https://github.com/axios/axios/issues/2082 Here is where I am using axios:

function onClick(e) {
  e.preventDefault();
  grecaptcha.ready(function() {
    grecaptcha.execute(secrets.recaptcahUrl, {action: 'submit'}).then(function(token) {
        // Add your logic to submit to your backend server here.
        axios.post(secrets.recaptcahUrl, {
          secret: secrets.recaptchaSecretKey,
          response: token
        })
        // logic to validate response token here
        .then(function (response) {
          console.log(response); //I wanted to log it first to see what is returned
        })
        .catch(function (error){
          console.log(error);
        });
    });
  });
}

and

app.post(home, (req, res) => {

  // onSubmit(this);
  const user = BuildNameAndEmail(req);
  onclick(res);
// I am doing more stuff but it is unrelated to axios

Any help would be sincerely appreciated.

CodePudding user response:

Your version of node won't work with the latest version of axios. Instead use an older version of the axios library. You can install it like this:

npm install --save-exact axios@^0.27.2

CodePudding user response:

I tried to find any official document related to your issue. Found nothing but a GitHub discussion that says:

We build for the lowest of v10, any lower than that is not guaranteed to work.

https://github.com/axios/axios/issues/2323

I guess this is the reason you are having this issue.

  • Related