Axios is not defined

I am using axios to build a simple weather app using React.js. I just finished the code but there is a problem. When I run this application it doesn't work at all and I see a reference error that says axios is not defined

.

Here is my webpack.config.js file:

module.exports = {
    entry: './public/app/app.jsx',
    output: {
        path: __dirname,
        filename: './public/js/bundle.js'
    },
    externals: ['axios'],
    resolve: {
        root: __dirname,
        alias: {
            OpenWeatherMap: 'public/components/OpenWeatherMap.jsx',
            Main: 'public/components/Main.jsx',
            Nav: 'public/components/Nav.jsx',
            Weather: 'public/components/Weather.jsx',
            WeatherForm: 'public/components/WeatherForm.jsx',
            WeatherMessage: 'public/components/WeatherMessage.jsx',
            About: 'public/components/About.jsx'
        },
        extensions: ['', '.js', '.jsx']
    },
    module: {
        loaders: [{
            loader: 'babel-loader',
            query: {
                presets: ['react','es2015', 'stage-0']
            },
            test: /\.jsx?$/,
            exclude: /(node_modules|bower_components)/
        },{
            loader: 'json-loader',
            test: /\.json?$/
        }]
    }
};

      

and the package.json file:

{
  "name": "weather",
  "version": "1.0.0",
  "description": "Simple Weather App",
  "main": "ext.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Milad Fattahi",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.16.1",
    "express": "^4.15.3",
    "json": "^9.0.6",
    "json-loader": "^0.5.4",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-router": "^4.1.1",
    "react-router-dom": "^4.1.1"
  },
  "devDependencies": {
    "babel-core": "^6.5.1",
    "babel-loader": "^6.2.2",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-stage-0": "^6.24.1",
    "webpack": "^1.12.13"
  }
}

      

+4


source to share


4 answers


I know this may sound obvious, but make sure there is a link to the correct axes at the top of your file or set it https://www.npmjs.com/package/axios



<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

      

+5


source


I forgot to import Axios. I added to the top of my script:

import axios from 'axios';

      

I also forgot to install axios. I installed axios via npm using:



npm i axios --global 
// --global flags will save you the stress of insstalling axios again.

      

This solved my mistake.

+2


source


i think first you have to install npm install axios --save

then import it into app.js file

import axios from "axios";

      

0


source


Just a reminder: be careful to never create variables with the same name as when importing.

0


source







All Articles