typescript 找不到模块:错误:无法解析“./lib/axios”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43877164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:32:07  来源:igfitidea点击:

Module not found: Error: Can't resolve './lib/axios'

typescriptwebpackaxios

提问by Gabriel Robert

I'm trying to import axios in my project. However, for some reasons, webpack/axios is causing me big troubles when trying to build.

我正在尝试在我的项目中导入 axios。然而,由于某些原因,webpack/axios 在尝试构建时给我带来了很大的麻烦。

GroupService.ts

组服务.ts

import axios, { AxiosResponse, AxiosInstance } from 'axios';

webpack --display-error-detailserrors

webpack --display-error-details错误

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 配置。

webpack.config.js

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
  ]
};

回答by Slawa Eremkin

Webpack can not find an entry point for axios, because try to search typescript file, but there is no such file. You should add .js to the resolve extensions. More information here: https://webpack.js.org/configuration/resolve/#resolve-extensions

webpack 找不到 axios 的入口点,因为尝试搜索 typescript 文件,但没有这样的文件。您应该将 .js 添加到解析扩展名中。更多信息:https: //webpack.js.org/configuration/resolve/#resolve-extensions

resolve: {
    extensions: ['.ts', '.js']
  },

回答by chervl

For me the problem was that I didn't restart the yarn startservice after installing axios the library was correctly installed in the app local node_modulesdirectory.

对我来说,问题是yarn start安装 axios 后我没有重新启动服务,库已正确安装在应用程序本地node_modules目录中。

Solution

解决方案

Stopped the yarn service with CTRL+Cand restarted it again with yarn start.

用 停止纱线服务CTRL+C并用 重新启动它yarn start

回答by Ish

I had the same problem and the following command solved my problem.

我遇到了同样的问题,以下命令解决了我的问题。

npm install --save axios

回答by Djesu

I had the same problem:

我有同样的问题:

module not found: Error: Can't resolve 'axios' in '

未找到模块:错误:无法解析 'axios'

I realized that axios folder in not found in the node_module folder of my project. I had already used npm install --save axiosbut it was still not found in my local node-module folder.

我意识到在我的项目的 node_module 文件夹中找不到 axios 文件夹。我已经使用过,npm install --save axios但在我的本地节点模块文件夹中仍未找到。

This is how I solved this issue. I looked for some other react projects I had already done and copied the axios folder in the node_module folder of that old project. I later pasted the copied axios folder into the node_module folder of my current project and the problem varnished.

这就是我解决这个问题的方法。我寻找了一些我已经完成的其他反应项目,并复制了该旧项目的 node_module 文件夹中的 axios 文件夹。后来我将复制的 axios 文件夹粘贴到我当前项目的 node_module 文件夹中,问题就解决了。