I'm trying to import axios in my project. However, for some reasons, webpack/axios is causing me big troubles when trying to build.
GroupService.ts
import axios, { AxiosResponse, AxiosInstance } from 'axios';
webpack --display-error-details errors
ERROR in ./~/axios/index.js
Module not found: Error: Can't resolve './lib/axios' in 'C:\dev\test-project\node_modules\axios'
resolve './lib/axios' in 'C:\dev\test-project\node_modules\axios'
using description file: C:\dev\test-project\node_modules\axios\package.json (relative path: .)
after using description file: C:\dev\test-project\node_modules\axios\package.json (relative path: .)
using description file: C:\dev\test-project\node_modules\axios\package.json (relative path: ./lib/axios)
as directory
C:\dev\test-project\node_modules\axios\lib\axios doesn't exist
no extension
C:\dev\test-project\node_modules\axios\lib\axios doesn't exist
.ts
C:\dev\test-project\node_modules\axios\lib\axios.ts doesn't exist
[C:\dev\test-project\node_modules\axios\lib\axios]
[C:\dev\test-project\node_modules\axios\lib\axios]
[C:\dev\test-project\node_modules\axios\lib\axios.ts]
@ ./~/axios/index.js 1:17-39
@ ./src/services/GroupService.ts
@ ./src/test-project.ts
Here's my webpack configurations.
webpack.config.js
/** Plugin settings **/
var jsOutputFile = 'dist/testproject.min.js';
var cssOutputFile = 'dist/styles.css';
/** Webpack configuration **/
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var extractCSS = new ExtractTextPlugin(cssOutputFile);
module.exports = {
// application entry file
entry: "./src/test-project.ts",
// bundled application output file
output: {
path: __dirname,
filename: jsOutputFile
},
// Currently we need to add '.ts' to the resolve.extensions array.
resolve: {
extensions: ['.ts']
},
// Source maps support ('inline-source-map' also works)
devtool: 'source-map',
// Add the loader for .ts files.
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader'
},
{
test: /\.scss$/,
use: extractCSS.extract([
{
loader: 'css-loader'
},
{
loader: 'sass-loader'
}
])
}
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
minimize: true,
sourceMap: true,
compress: {
warnings: true
}
}),
extractCSS
]
};
Axios is an HTTP client library based on promises. It makes sending asynchronous HTTP requests to REST endpoints easier and helps you perform CRUD operations. This REST endpoint/API could be an external API like the Google API, GitHub API, and so on – or it could be your own backend Node. js server.
For me the problem was that I didn't restart the yarn start
service after installing axios the library was correctly installed in the app local node_modules
directory.
Stopped the yarn service with CTRL+C and restarted it again with yarn start
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With