Getting error on running command `npm run build`, error `TypeError: MiniCssExtractPlugin is not a constructor`
Asked Answered
I

2

11

I'm creating an app using react it runs fine on npm start but when I try to build the app, this show the following error.

PS D:\ ****\ **\*\profile> npm run build

> [email protected] build
> react-scripts build

D:\ ****\ **\profile\node_modules\react-scripts\config\webpack.config.js:664
        new MiniCssExtractPlugin({
        ^

TypeError: MiniCssExtractPlugin is not a constructor
    at module.exports (D:\Documents\Project\React\profile\node_modules\react-scripts\config\webpack.config.js:664:9)
    at Object.<anonymous> (D:\Documents\Project\React\profile\node_modules\react-scripts\scripts\build.js:58:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

I'm using

Package name Version
node 16.13.2
npm 8.1.2
npx 8.1.2

This is my package.json file.

{
  "name": "profile",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "5.0.0",
    "web-vitals": "^2.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I also tried to re-install whole node package, but I'm getting this error again and again.

Irresoluble answered 15/1, 2022 at 7:42 Comment(0)
B
12

There is an update to mini-css-extract-plugin in version 2.5.0. I temporarily fixed it by adding in package.json:

 "overrides": {
    "mini-css-extract-plugin": "2.4.5"
  }

for your npm version first try:

npm i -D --save-exact [email protected]
Bevel answered 15/1, 2022 at 8:50 Comment(2)
You can check more discussion on this at github it is fresh: GithubBevel
How would you get past this when deploying to Heroku?Heathenry
C
0

In file package-lock.json changed mini-css-extract-plugin to 2.4.5 in three places: 1)

"mini-css-extract-plugin": {
      "version": "2.4.5",
      "resolved": "https://registry.npmjs.org/mini-css-extract-plugin/-/mini-css-extract-plugin-2.4.5.tgz",
      "integrity": "sha512-oEIhRucyn1JbT/1tU2BhnwO6ft1jjH1iCX9Gc59WFMg0n5773rQU0oyQ0zzeYFFuBfONaRbQJyGoPtuNseMxjA==",
      "requires": {
        "schema-utils": "^4.0.0"
      },
"node_modules/mini-css-extract-plugin": {
      "version": "2.4.5",
      "resolved": "https://registry.npmjs.org/mini-css-extract-plugin/-/mini-css-extract-plugin-2.4.5.tgz",
      "integrity": "sha512-oEIhRucyn1JbT/1tU2BhnwO6ft1jjH1iCX9Gc59WFMg0n5773rQU0oyQ0zzeYFFuBfONaRbQJyGoPtuNseMxjA==",
      "dependencies": {
        "schema-utils": "^4.0.0"
      }, 
  1. also at the bottom of the file:
"mini-css-extract-plugin": "^2.4.5",
  1. At top level in project folder (to override the older folder) run

npm install [email protected]

As a result 'npm run build' works for me

Cowey answered 15/1, 2022 at 21:56 Comment(1)
Note, that editing lock files is a bad practice by many reasons, use overrides approach from the accepted answer.Cringe

© 2022 - 2024 — McMap. All rights reserved.