Search code examples
reactjspathwebpackoutputdevserver

Error: output.path needs to be an absolute path or /


This is my webpack.config file. When I run webpack, bundle.js correctly writes to project/dist/assets/bundle.js.

However, when I run npm start to serve up the files, I get an error:

Error: output.path needs to be an absolute path or /.

So... if I make the path absolute: "/dist/assets" or path: __dirname + "/dist/assets" then it serves up the files fine, and emits bundle.js, but it doesn't actually write it to my project/dist/assets folder.

The page looks fine and when I view source, I see <script src="/assets/bundle.js"></script> but it only exists on the localhost (publicPath).

Where am I going wrong? The goal being for npm start to write the bundle to my project folder AND serve it up with devServer.

var webpack = require("webpack");

module.exports = {
  entry: "./src/index.js",
  output: {
    path: "./dist/assets",
    filename: "bundle.js",
    publicPath: "/assets"
  },
  devServer: {
    inline: true,
    contentBase: "./dist",
    port: 3000
  },
  module: {
    loaders: [{
      test: /\.js$/,
      exclude: /(node_modules)/,
      loader: ["babel-loader", "babel-loader?presets[]=latest,presets[]=stage-0,presets[]=react"]
    }, {
      test: /\.json$/,
      exclude: /(node_modules)/,
      loader: "json-loader"
    }, {
      test: /\.css$/,
      loader: "style-loader!css-loader!autoprefixer-loader"
    }, {
      test: /\.scss$/,
      loader: "style-loader!css-loader!autoprefixer-loader!sass-loader"
    }]
  }
}

Solution

  • Use path for this:

    var path = require('path');
    
    ....
    
    output: {
      path: path.join(__dirname, './dist/assets'),