Getting “Error: `output.path` needs to be an absolute path or `/`”

后端 未结 4 2017
离开以前
离开以前 2020-12-01 01:30

I am new to JS development, in an attempt to hot load changes using webpack-dev-server I keep above exception. The exact stack is:

Error: `output.path` needs         


        
4条回答
  •  误落风尘
    2020-12-01 02:23

    You could use __dirname to get current executed file's path.

    const webpack = require('webpack')
    
    module.exports = {
        mode: 'development',
        entry: __dirname + "/src/index.js",
        output: {
            path: __dirname + "/dist",
            filename: 'main.js'
        }
    }
    

    You could also import build-in path API & use resolve method, i think this one is prefer by webpack

    const webpack = require('webpack')
    const path = require('path')
    
    module.exports = {
        mode: 'development',
        entry:  path.resolve("./src/index.js"),
        output: {
            path: path.resolve("./dist"),
            filename: 'main.js'
        }
    }
    

提交回复
热议问题