2017-02-10 66 views
19

我新的JS發展,企圖利用的WebPack-dev的服務器我把上面的異常熱負荷的變化。確切的堆棧:獲得「錯誤:`output.path`需要是絕對路徑或`/`」

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

回答

51

正如錯誤信息所說,你ne編輯使用絕對路徑。

要獲取當前目錄的絕對路徑,您可以使用__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模塊。

從文檔報價:

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.

您可以在webpack.config.js頂部需要它作爲

var path = require('path'); 
..... 
.... 
.. 
output: { 
    path: path.join(__dirname, "dist/js"), 
    filename: "bundle.js" 
} 
// rest of the configuration 
從上面的兩種方法

除此之外,您還可以使用path.resolve提到here

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

希望它能幫助:)

+0

工作就像一個魅力!謝謝@hardik .. –

+0

由於某些原因,我仍然得到了與路徑相同的異常:「./dist/js」,而__dirname +「dist/js」正常工作。 –

+0

'。/ dist/js'是相對路徑。可能是這就是你出錯的原因。但在我的項目中工作正常。你正在使用哪個版本的webpack? –

-1

您可以使用像下面的代碼獲取絕對路徑。

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

不要在路徑要求。這應該在文件的頂部定義,以便可以引用其他路徑。這是不好的做法。 –

0

您需要包括這在頂部或webpack.config.js文件var path = require('path') ,然後在你的路徑執行以下操作:path: path.join(__dirname, "dist/js")