Home > Software engineering >  Vue.config.js could not recognize require('webpack')
Vue.config.js could not recognize require('webpack')

Time:12-13

I create a vue3.0 ts project by CLI, when I add vue.config.js, it always have error on require

const webpack = require('webpack');
const path = require('path');

The errors are

module "d:/learning/vue-3-ts-demo/node_modules/webpack/types"

Require statement not part of import statement`.

I have install webpack 5.65.0

My packages.json:

{
  "name": "vue-3-ts-demo",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^3.6.5",
    "vue": "^3.0.0",
    "vue-router": "^4.0.0-0",
    "vuex": "^4.0.0-0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.18.0",
    "@typescript-eslint/parser": "^4.18.0",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-typescript": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "@vue/eslint-config-typescript": "^7.0.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.0.0",
    "sass": "^1.26.5",
    "sass-loader": "^8.0.2",
    "typescript": "~4.1.5",
    "webpack": "^5.65.0"
  }
}

It is a new vue project and what is the problem?

CodePudding user response:

The errors are module "d:/learning/vue-3-ts-demo/node_modules/webpack/types"

Require statement not part of import statement`.

For this error, I delt with it by adding '@typescript-eslint/no-var-requires': 0 in.eslintrc.js

rules: {
'@typescript-eslint/no-var-requires': 0
}

I have install webpack 5.65.0

Since my vue CLI is 4.5.14, I reinstall webpack version as 4.x too, and it works finally.

  • Related