webpack error while running npm run build

Issue

I’m trying to install in vanilla typescript axios for HTTP Request, I use webpack as bundler but when I try to run my webpack I get error

[webpack-cli] Failed to load 'C:\Users\lenovo\OneDrive\Desktop\Web Dev Stuff\Weather-API\webpack.config.js' config
[webpack-cli] ReferenceError: path is not defined
    at Object.<anonymous> (C:\Users\lenovo\OneDrive\Desktop\Web Dev Stuff\Weather-API\webpack.config.js:17:11)
    at Module._compile (C:\Users\lenovo\OneDrive\Desktop\Web Dev Stuff\Weather-API\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    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 (C:\Users\lenovo\OneDrive\Desktop\Web Dev Stuff\Weather-API\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)
    at WebpackCLI.tryRequireThenImport (C:\Users\lenovo\OneDrive\Desktop\Web Dev Stuff\Weather-API\node_modules\webpack-cli\lib\webpack-cli.js:32:22)
    at loadConfig (C:\Users\lenovo\OneDrive\Desktop\Web Dev Stuff\Weather-API\node_modules\webpack-cli\lib\webpack-cli.js:1536:38)
    at WebpackCLI.resolveConfig (C:\Users\lenovo\OneDrive\Desktop\Web Dev Stuff\Weather-API\node_modules\webpack-cli\lib\webpack-cli.js:1639:44)

also this is my webpack.config.json

module.exports = {
  entry: './src/app.ts',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, 'public/dist'),
  },
};

and this is my package.json to run my webpack

{
  "name": "weather-api",
  "version": "1.0.0",
  "description": "weather-api",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "server": "webpack-dev-server"
  },
  "author": "callmenik",
  "license": "ISC",
  "devDependencies": {
    "ts-loader": "^9.2.3",
    "webpack": "^5.39.1",
    "webpack-cli": "^4.7.2"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "date.js": "^0.3.3",
    "datejs": "^1.0.0-rc3"
  }
}

what do I do wrong?

Solution

You forgot to include in your webpack.config.jsona path

const path = require('path');

include that and it will run well

Answered By – callmenikk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published