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

MAR*_*att 11 typescript webpack 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;
Run Code Online (Sandbox Code Playgroud)

它是一个打字稿项目.

当我使用webpack版本4.5.0运行build并缩小typescript项目时,我收到以下错误

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

不确定什么是错的.

我使用ts-loader版本4.2.0

打字稿版本2.8.1

好心劝告.

Dav*_*han 13

我刚刚安装了ts-loader,问题就消失了:

npm install -D ts-loader       
Run Code Online (Sandbox Code Playgroud)

 module: {
    rules: [
      {test: /\.ts$/, exclude: /node_modules/, loader: 'ts-loader'}
    ]
  },
Run Code Online (Sandbox Code Playgroud)

  • 这个答案没有解释为什么 npm 在 package.json 中存在 ts-loader 时没有安装它。 (6认同)