Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios and Webpack

Tags:

npm

webpack

axios

I'm really new to Webpack and I'm using axios with a React project. I installed axios via npm and then I'm importing it like so when I want to use it:

import axios from 'axios/dist/axios.min.js';

Webpack takes care of the rest. Is this the "right" way to do it?

like image 689
Gregg Avatar asked Aug 09 '16 17:08

Gregg


People also ask

What is Axios package used for?

Axios: Axios is a Javascript library used to make HTTP requests from node. js or XMLHttpRequests from the browser and it supports the Promise API that is native to JS ES6. It can be used intercept HTTP requests and responses and enables client-side protection against XSRF. It also has the ability to cancel requests.

What is webpack in react?

Like create-React-app, React Webpack is also a command-line tool used to create a bundle of assets (files and code). It doesn't run on the browser or the server. It takes all the JS files and other assets, transforming them into one large file.

What is webpack used for?

Webpack is a free and open-source module bundler for JavaScript. It is made primarily for JavaScript, but it can transform front-end assets such as HTML, CSS, and images if the corresponding loaders are included. Webpack takes modules with dependencies and generates static assets representing those modules.

What is NPM Axios?

Axios is a promise based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.


1 Answers

I think the standard way of doing this is as follows:

import axios from 'axios';

The UMD build (axios.min.js) can be helpful when you need to include axios in a <script> tag:

<script src="https://npmcdn.com/axios/dist/axios.min.js"></script>
like image 150
Nick Uraltsev Avatar answered Oct 11 '22 12:10

Nick Uraltsev