得到“错误:`output.path`需要是一个绝对路径或`/`”

时间:2017-02-10 18:42:19

标签: webpack webpack-dev-server

我是JS开发的新手,试图使用webpack-dev-server热插拔更改我保持异常。确切的堆栈是:

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)

以下是我已经尝试过的webpack配置文件:

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/
            }
        ]
    }
};

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']
                }
            }
        ]
    }
};

以下是我的package.json文件

{
 "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": ""
}

4 个答案:

答案 0 :(得分:68)

如错误消息所示,您需要使用绝对路径。

要获取当前目录的绝对路径,可以使用__dirname获取当前目录,然后附加dist/js。 所以它就像是,

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

两者都可以正常工作。您可以阅读有关webpack配置here

的信息

修改:要使用path: path.join(__dirname, "dist/js"),您需要使用节点内置的path模块。

引用文档:

  

路径模块:   它提供了处理文件和目录路径的实用程序。将它与前缀__dirname全局一起使用将防止操作系统之间的文件路径问题,并允许相对路径按预期工作。

您可以在webpack.config.js的顶部将其作为

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

除上述两种方法外,您还可以使用path.resolve here。{/ p>

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

希望有所帮助:)

答案 1 :(得分:0)

你需要在顶部或webpack.config.js文件var path = require('path')中包含它 然后在您的路径中执行以下操作:path: path.join(__dirname, "dist/js")

答案 2 :(得分:0)

您可以使用__dirname来获取当前执行文件的路径。

const webpack = require('webpack')

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

您还可以导入内置的path API并使用resolve方法,我认为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'
    }
}

答案 3 :(得分:-1)

您可以像下面的代码一样使用它来获取绝对路径。

output: {
  path: require('path').resolve("./dist/js"),
  filename: 'bundle.js',
  publicPath: 'http://127.0.0.1:2992/js'
}