Module not found: Error: Can't resolve 'ts-loader'

Module not found: Error: Can't resolve 'ts-loader'

以下是我的webpack.config.js

    var webpack = require('webpack'),
  path = require('path'),
  yargs = require('yargs');

var libraryName = 'MyLib',
  plugins = [],
  outputFile;

if (yargs.argv.p) {
  plugins.push(new webpack.optimize.UglifyJsPlugin({
    minimize: true
  }));
  outputFile = libraryName + '.min.js';
} else {
  outputFile = libraryName + '.js';
}

var config = {
  entry: [
    __dirname + '/src/TestClass.ts'
  ],
  devtool: 'source-map',
  output: {
    path: path.join(__dirname, '/dist'),
    filename: outputFile,
    library: libraryName,
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  module: {
    rules: [{
      test: /\.tsx?$/,
      use: 'ts-loader', //Something wrong here
      exclude: /node_modules/
    }]
  },
  resolve: {
    modules: [__dirname],
    extensions: ['.ts', '.js', '.tsx']
  },
  plugins: plugins

      };

module.exports = config;

这是一个打字稿项目。

当我运行 使用 webpack 版本 4.5.0 构建和缩小 typescript 项目时,出现以下错误

未找到模块:错误:无法解析 'ts-loader'

不知道怎么了。

我使用的是 ts-loader 版本 4.2.0

打字稿版本 2.8.1

请指教。

确保您已安装 ts-loader 并将其添加到规则中:

npm install -D ts-loader       

 module: {
    rules: [
      {test: /\.ts$/, exclude: /node_modules/, loader: 'ts-loader'}
    ]
  },

同样如此,我已经在我的 package.json 中列出了 ts-loader,但由于某些原因它在某些时候不再安装了。

对我来说,有效的方法是我强制重新安装所有模块,它出现并再次起作用。

npm i -force

yarn install --force