1

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"
    }]
  }
}
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
Kirk Ross
  • 6,413
  • 13
  • 61
  • 104

1 Answers1

0

Use path for this:

var path = require('path');

....

output: {
  path: path.join(__dirname, './dist/assets'),
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
  • Thank you. This works for devServer but I still need to run webpack separately to actually write the file. How would I configure it so npm start runs webpack and also serves to devServer? – Kirk Ross Mar 03 '17 at 05:13
  • define the script in `package.json` for `npm start`, it will run the `wepack` for you, include this line in `package.json`: `"scripts": { "start": "webpack-dev-server --color --inline"}` – Mayank Shukla Mar 03 '17 at 05:49
  • Hmmm... it still doesn't run webpack (and write the file) unless I do it separate from npm start. I have this in my package.json {... "scripts": { "start": "webpack-dev-server --color --inline" } – Kirk Ross Mar 03 '17 at 07:32