简体   繁体   English

类型错误:CleanwebpackPlugin 不是构造函数

[英]TypeError: CleanwebpackPlugin is not a constructor

i'm trying to preview a vue web application through webpack-server-dev.I'm following this guide https://medium.com/the-web-tub/creating-your-first-vue.js-pwa-project-22f7c552fb34我正在尝试通过 webpack-server-dev 预览 vue web 应用程序。我正在遵循本指南https://medium.com/the-web-tub/creating-your-first-vue.js-pwa-project -22f7c552fb34

The guide explains that the plugin is used to delete old and unused files in the dist directory.该指南解释说,该插件用于删除 dist 目录中的旧文件和未使用的文件。 I have already tried replacing const CleanWebpackPlugin = require('clean-webpack-plugin') with const { CleanWebpackPlugin } = require('clean-webpack-plugin') which some posts suggest.我已经尝试用一些帖子建议const { CleanWebpackPlugin } = require('clean-webpack-plugin')替换const CleanWebpackPlugin = require('clean-webpack-plugin') ) 。 i have also tried looking at the documentation on https://github.com/johnagan/clean-webpack-plugin but without succes as i am pretty new to this.我也尝试查看https://github.com/johnagan/clean-webpack-plugin上的文档,但没有成功,因为我对此很陌生。

when i try to npm run dev i get this error当我尝试npm run dev时,我得到了这个错误

    new CleanWebpackPlugin(['dist']),
    ^

TypeError: CleanWebpackPlugin is not a constructor
    at module.exports (C:\Users\Eson\Desktop\pwa-vue-app-1\webpack.config.js:56:5)
    at handleFunction (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\prepareOptions.js:21:13)
    at prepareOptions (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\prepareOptions.js:9:5)
    at requireConfig (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\convert-argv.js:119:14)
    at C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\convert-argv.js:125:17
    at Array.forEach (<anonymous>)
    at module.exports (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\convert-argv.js:123:15)
    at Object.<anonymous> (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-dev-server\bin\webpack-dev-server.js:79:40)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

and this is the webpack.config.js file这是 webpack.config.js 文件

const path = require('path')

const { VueLoaderPlugin } = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')

module.exports = (env, argv) => ({
  mode: argv && argv.mode || 'development',
  devtool: (argv && argv.mode || 'development') === 'production' ? 'source-map' : 'eval',

  entry: './src/app.js',

  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },

  node: false,

  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js$/,
        loader: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
        exclude: /\.module\.css$/
      }
    ]
  },

  resolve: {
    extensions: [
      '.js',
      '.vue',
      '.json'
    ],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': path.resolve(__dirname, 'src')
    }
  },

  plugins: [
    new CleanWebpackPlugin(['dist']),
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'static', 'index.html'),
      inject: true
    }),
    new CopyWebpackPlugin([{
      from: path.resolve(__dirname, 'static'),
      to: path.resolve(__dirname, 'dist'),
      toType: 'dir'
    }])
  ],

  optimization: {
    splitChunks: {
      chunks: 'all',
      minSize: 30000,
      maxSize: 0,
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        }
      }
    },
    runtimeChunk: {
      name: entrypoint => `runtime~${entrypoint.name}`
    },
    mangleWasmImports: true,
    removeAvailableModules: true,
    removeEmptyChunks: true,
    mergeDuplicateChunks: true
  },

  devServer: {
    compress: true,
    host: 'localhost',
    https: true,
    open: true,
    overlay: true,
    port: 9000
  }
});

this is the error i get when using the right import as explained in the documenation:这是我在使用文档中解释的正确导入时遇到的错误:

      throw new Error(`clean-webpack-plugin only accepts an options object. See:
      ^

Error: clean-webpack-plugin only accepts an options object. See:
            https://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional
    at new CleanWebpackPlugin (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\clean-webpack-plugin\dist\clean-webpack-plugin.js:27:13)
    at module.exports (C:\Users\Eson\Desktop\pwa-vue-app-1\webpack.config.js:56:5)
    at handleFunction (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\prepareOptions.js:21:13)
    at prepareOptions (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\prepareOptions.js:9:5)
    at requireConfig (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\convert-argv.js:119:14)
    at C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\convert-argv.js:125:17
    at Array.forEach (<anonymous>)
    at module.exports (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-cli\bin\utils\convert-argv.js:123:15)
    at Object.<anonymous> (C:\Users\Eson\Desktop\pwa-vue-app-1\node_modules\webpack-dev-server\bin\webpack-dev-server.js:79:40)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19) 

if i delete line 56 in webpack.config.js i can run the web application without problems, but i want to understand the source of this issue如果我删除 webpack.config.js 中的第 56 行,我可以毫无问题地运行 web 应用程序,但我想了解此问题的根源

The correct one is to use this import:正确的是使用这个导入:

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

And then instead of passing an array with the distribution folder, change it to然后不是通过分发文件夹传递数组,而是将其更改为

plugins: [
     new CleanWebpackPlugin(),
     //...
]

I had the same problem, and I solved it in the following way:我遇到了同样的问题,我通过以下方式解决了它:


    const { CleanWebpackPlugin } = require('clean-webpack-plugin');


    plugins: [
        new CleanWebpackPlugin({
            cleanAfterEveryBuildPatterns: ['dist']
        })
    ]

I had the same issue today, right now.我今天遇到了同样的问题,现在。 As you can tell, there was a mismatch between the docs and the actual code.如您所知,文档和实际代码之间存在不匹配。 And in fact, you can see in the last commit they merged both to the documentation:事实上,你可以在最后一次提交中看到它们合并到文档中:

在此处输入图片说明

and also to the code:还有代码:

在此处输入图片说明

So I just switched from const CleanWebpackPlugin = require('clean-webpack-plugin') to所以我只是从const CleanWebpackPlugin = require('clean-webpack-plugin')切换到

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

and it works fine.它工作正常。

I think you may have been caught in between things.我想你可能被夹在中间。 Reinstall the npm package and try again, it should work.重新安装 npm 包并重试,它应该可以工作。

I wrote a bit of what you can see in their public repository because very often when sudden changes like this happen, you'll find your own answer in the repo , probably in the last commits.我写了一些您可以在他们的公共存储库中看到的内容,因为经常发生这样的突然更改时,您会在 repo 中找到自己的答案,可能是在最后一次提交中。 And it's good reading a bit of the code you use, especially if it helps you troubleshoot your issue :)阅读您使用的一些代码会很好,特别是如果它可以帮助您解决问题:)

With the update you'll need to do the following to include it通过更新,您需要执行以下操作以包含它

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

Then in the array of plugins replace add the following然后在插件数组中替换添加以下内容

plugins: [
     new CleanWebpackPlugin(['dist]),
]

with

plugins: [
     new CleanWebpackPlugin(),
]

As the with the update there is no need to pass any parameters as it will remove all files inside webpack's output.path directory, as well as all unused webpack assets after every successful rebuild.与更新一样,不需要传递任何参数,因为它会在每次成功重建后删除 webpack 的output.path目录中的所有文件,以及所有未使用的 webpack 资产。

For those finding this error with the recent updates to nativescript-vue , I fixed it by changing webpack.config.js (top level file in app folder).对于最近对nativescript-vue 的更新发现此错误的人,我通过更改webpack.config.js (应用程序文件夹中的顶级文件)来修复它。 As above, it now reflects the syntax in the CleanWebpackPlugin docs .如上所述,它现在反映了 CleanWebpackPlugin docs 中的语法。

//const CleanWebpackPlugin = require("clean-webpack-plugin");
  const { CleanWebpackPlugin } = require('clean-webpack-plugin');

and

//new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
  new CleanWebpackPlugin(),

When import, use { CleanWebpackPlugin } instead of CleanWebpackPlugin导入时,使用 { CleanWebpackPlugin } 而不是 CleanWebpackPlugin

This happens when the version is more than 1.当版本大于 1 时会发生这种情况。

Example:例子:

const { CleanWebpackPlugin } = require("clean-webpack-plugin");

CleanWebpackPlugin v3.0.0 CleanWebpackPlugin v3.0.0

Replaced default export with named export CleanWebpackPlugin将默认导出替换为命名导出 CleanWebpackPlugin

[ https://github.com/johnagan/clean-webpack-plugin/releases/tag/v3.0.0] [ https://github.com/johnagan/clean-webpack-plugin/releases/tag/v3.0.0]

correct code is:正确的代码是:

// es modules
import { CleanWebpackPlugin } from 'clean-webpack-plugin';

// common js
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

I am not very much familiar with webpack and currently learning it我对 webpack 不是很熟悉,目前正在学习它

although this thing below helped me fix the issue虽然下面的这个东西帮助我解决了这个问题

I just uninstall clean-webpack-plugin and then reinstall as dependency before this i've intalled as a dev-dependency我只是卸载 clean-webpack-plugin 然后重新安装为依赖项,然后我将其安装为开发依赖项

after uninstall and installing it like that : npm install --save clean-webpack-plugin像这样卸载并安装后: npm install --save clean-webpack-plugin

and by adding this并通过添加这个

const { CleanWebpackPlugin } = require("clean-webpack-plugin");

solved my issue!!解决了我的问题!!

hope it helps希望能帮助到你

I had the same issue today, I figured out that I also had to remove webpack.config.js file.我今天遇到了同样的问题,我发现我还必须删除webpack.config.js文件。

After deleting the file, i had to rerun npm install删除文件后,我不得不重新运行npm install

-> Application started successfully. -> 应用程序成功启动。

-- ——

Inside webpack.config.js some old references were placed.webpack.config.js中放置了一些旧引用。

This is probably a weird outlier reason for this - but I just came across this while setting up an old project on a new linux machine.这可能是一个奇怪的异常原因 - 但我只是在新的 linux 机器上设置旧项目时遇到了这个问题。 It turned out I didn't have dev dependencies installed for my project (by default if npm config is set to production it won't install dev dependencies when running npm install - although I'm not sure exactly why mine was set to production), my webpack cli is 4 but my project is at 2.6.1 so it was using the webpack 4 but my webpack.config was for 2. So make sure your dev dependencies are installed.原来我没有为我的项目安装开发依赖项(默认情况下,如果 npm config 设置为生产,它在运行 npm install 时不会安装开发依赖项 - 尽管我不确定为什么我的设置为生产) ,我的 webpack cli 是 4,但我的项目是 2.6.1,所以它使用的是 webpack 4,但我的 webpack.config 是 2。所以确保你的开发依赖项已安装。

Webpack v5 doesn't need cleanwebpack. Webpack v5 不需要 cleanwebpack。 Use output.clean: "true".使用 output.clean:“真”。

Be also aware of writing it like this也要注意这样写

const {
    CleanWebpackPlugin
} = require('clean-webpack-plugin');

and not like this :而不是这样:

const {
    cleanWebpackPlugin
} = require('clean-webpack-plugin');

In my case that small letter C was an issue.就我而言,小写字母 C 是一个问题。 It is class name of this plugin which is being imported, so there is no place for arbitrariness in naming.导入的是这个插件的类名,所以命名不能随意。

Looks like docs are broken, correct code is看起来文档坏了,正确的代码是

const CleanWebpackPlugin = require('clean-webpack-plugin') const CleanWebpackPlugin = require('clean-webpack-plugin')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM