Dolphin
Dolphin

Reputation: 38885

ReferenceError: resolve is not defined after added the @ path config

I want to use @ with the import path like this in vue 3:

import Options from '@/popup/components/Options.vue'

so I added this config in the webpack 5.x:

'@': resolve('src'),

this is the full webpack 5.x config:

const path = require('path');
const webpack = require( 'webpack' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin');
const HtmlWebpackPlugin = require( 'html-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin");
const { VueLoaderPlugin } = require("vue-loader");

module.exports = {
  entry : {
    'popup/popup' : './src/popup/' 
  } ,
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    alias: {
        // https://stackoverflow.com/questions/50805384/module-not-found-error-cant-resolve-vue-path-not-correct
        vue: 'vue/dist/vue.esm-bundler.js',
        '@': resolve('src'),
    },
  },
  output : {
    path : path.resolve(__dirname, '../../bundle') ,
    filename : '[name].js'
  },
  module : {
    rules : [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        options: {
          appendTsSuffixTo: [/\.vue$/]
        },
        exclude: /node_modules/
      },
      
      {
        test : /\.js$/ ,
        exclude : [ /node_modules(?!(\/|\\?\\)(translation\.js|selection-widget|connect\.io|chrome-env)\1)/ ] ,
        loader : 'babel-loader'
      } ,
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
      {
        test : /\.(scss)$/ ,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
      }
    ]
  },
  plugins : [
    new VueLoaderPlugin(),
    new CopyPlugin({
      patterns: [
        { from: "src/manifest.json", to: "manifest.json" },
        { from: "src/resource/image", to: "resource/image" },
      ],
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css",
    }),
    new HtmlWebpackPlugin({
      filename: 'popup/popup.html',
      template: 'src/popup/index.html'
    }),
    new webpack.DefinePlugin({
      __VUE_OPTIONS_API__: false,
      __VUE_PROD_DEVTOOLS__: false,
    }),
  ]
};

but the compile output error like this:

[webpack-cli] Failed to load '/Users/xiaoqiangjiang/source/reddwarf/frontend/reddwaf-translate-plugin/src/resource/config/webpack.dev.config.js' config
[webpack-cli] ReferenceError: resolve is not defined
    at Object.<anonymous> (/Users/xiaoqiangjiang/source/reddwarf/frontend/reddwaf-translate-plugin/src/resource/config/webpack.base.config.js:17:14)
    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 Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/xiaoqiangjiang/source/reddwarf/frontend/reddwaf-translate-plugin/src/resource/config/webpack.dev.config.js:3:10)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)

where is the problem? what should I do to fix this?

Upvotes: 5

Views: 3090

Answers (1)

vighnesh153
vighnesh153

Reputation: 5386

On line 17 of your webpack.config.js, you are referencing resolve. It is a part of the path module. So, you need to do the following instead:

'@': path.resolve(__dirname, 'src')

Upvotes: 5

Related Questions