31

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 to be an absolute path or `/`.
at Object.Shared.share.setFs (/Users/mybox/work/day1/ex6/node_modules/webpack-dev-middleware/lib/Shared.js:88:11)
at Shared (/Users/mybox/work/day1/ex6/node_modules/webpack-dev-middleware/lib/Shared.js:214:8)
at module.exports (/Users/mybox/work/day1/ex6/node_modules/webpack-dev-middleware/middleware.js:22:15)
at new Server (/Users/mybox/work/day1/ex6/node_modules/webpack-dev-server/lib/Server.js:56:20)

at startDevServer (/Users/mybox/work/day1/ex6/node_modules/webpack-dev-server/bin/webpack-dev-server.js:379:12)
at processOptions (/Users/mybox/work/day1/ex6/node_modules/webpack-dev-server/bin/webpack-dev-server.js:317:3)
at Object.<anonymous> (/Users/mybox/work/day1/ex6/node_modules/webpack-dev-server/bin/webpack-dev-server.js:441:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)

Here are the webpack config files i have tried already:

module.exports = {
    entry: "./client/app.jsx",
    output: {
        path: "dist/js",
        filename: "bundle.js",
        publicPath: "http://127.0.0.1:2992/js"
    },
    module: {
        loaders: [
            {
                test: /.jsx?$/,
                loader: "babel-loader",
                include: /client/
            }
        ]
    }
};

And:

module.exports = {
    entry: "./client/app.jsx",
    output: {
        path: "/Users/mybox/work/day1/ex6/dist/js",
        filename: "bundle.js",
        publicPath: "http://127.0.0.1:2992/js"
    },
    module: {
        loaders: [
            {
                test: /.jsx?$/,
                loader: "babel-loader",
                include: /client/,
                query: {
                    presets:['react']
                }
            }
        ]
    }
};

Below is my package.json file

{
 "name": "ex6",
 "version": "1.0.0",
 "main": "index.js",
 "scripts": {
   "server": "node index.js",
   "hot": "webpack-dev-server --inline --hot --port 2992 --progress --colors",
   "dev": "webpack-dev-server --inline --dev --port 2992 --progress --colors"
 },
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
  "babel-preset-es2015": "^6.22.0",
  "hapi": "^16.1.0",
  "inert": "^4.1.0"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.22.2",
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-preset-react": "^6.22.0",
"builder": "^3.2.1",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
},
"description": ""
}
halfer
  • 19,824
  • 17
  • 99
  • 186
bluefalcon
  • 505
  • 1
  • 5
  • 21

5 Answers5

78

As the error message says, you need to use absolute path.

To get an absolute path for current directory, You can use __dirname to get the current directory and then append dist/js. So it would be something like,

output: {
    path: __dirname + "/dist/js", // or path: path.join(__dirname, "dist/js"),
    filename: "bundle.js"
}

Both will work just fine. You can read about webpack configuration here

Edit: To use path: path.join(__dirname, "dist/js") you will need to require node's built-in path module.

Quoting from the docs:

Path module: It provides utilities for working with file and directory paths. Using it with the prefix __dirname global will prevent file path issues between operating systems and will allow relative paths to work as expected.

You can require it at the top of your webpack.config.js as

var path = require('path');
.....
....
..
output: {
    path: path.join(__dirname, "dist/js"),
    filename: "bundle.js"
}
// rest of the configuration

Apart from above two methods, You can also use path.resolve as mentioned here.

path: path.resolve(__dirname, "dist/js")

Hope it helps :)

Hardik Modha
  • 12,098
  • 3
  • 36
  • 40
  • For some reason i am still getting the same exception with path: "./dist/js", while __dirname + "dist/js" works fine. – bluefalcon Feb 10 '17 at 19:17
  • 1
    `./dist/js` is relative path. May be that's why you are getting an error. But in my project it's working fine. Which version of webpack you are using? – Hardik Modha Feb 10 '17 at 19:19
  • webpack": "^2.2.1", "webpack-dev-server": "^2.3.0" – bluefalcon Feb 10 '17 at 19:23
  • 1
    Why should this be done? Why does it need a abs path? Is there a specific reason such design decision was made? – hungryWolf Apr 06 '18 at 09:41
  • 2
    @hungryWolf I don't know about why this design decision was made but it is a limitation and a consistency issue for `webpack` users. We'll soon be able to use the relative path value for `output.path`. You can visit this PR for details: https://github.com/webpack/webpack/pull/6214 – Hardik Modha Apr 06 '18 at 10:26
  • (cont. from hungryWolf's question) In the docs, https://webpack.js.org/configuration/output/#outputpath, the absolute path is sill required? – NeoZoom.lua Apr 03 '22 at 02:42
0

you need to include this at the top of or webpack.config.js file var path = require('path') and then in your path do the following: path: path.join(__dirname, "dist/js")

Michael Guild
  • 808
  • 9
  • 8
0

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'
    }
}
Aung Zan Baw
  • 392
  • 2
  • 8
0

It's a design joke called aymmetric system-inconsistency from the author, you can find more details from the issue there: https://github.com/webpack/webpack/issues/1908.

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
-2

You can use it like following code to get absolute path.

output: {
  path: require('path').resolve("./dist/js"),
  filename: 'bundle.js',
  publicPath: 'http://127.0.0.1:2992/js'
}
  • 4
    don't require in the path. That should be defined at the top of the file so it can be referenced for other paths. That's bad practice. – Michael Guild Feb 13 '17 at 15:42