2017-02-10 26 views
19

webpack-dev-serverを使用して変更をホットロードしようとすると、JS開発の新機能です。正確なスタックは、次のとおりです。「エラー: `output.path`は絶対パスまたは`/`でなければなりません。

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": "" 
} 
ですがここで

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の設定ファイルです

答えて

51

エラーメッセージには、絶対パスを使用するように編集しました。

現在のディレクトリの絶対パスを取得するには、__dirnameを使用して現在のディレクトリを取得し、dist/jsを追加します。 だから、両方がうまく動作します何かのように、

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

だろう。あなたは編集

here WebPACKの構成について読むことができます:あなたは、ノードのビルトインpathモジュールを必要とする必要がありますpath: path.join(__dirname, "dist/js")を使用するには。

ドキュメントからの引用:hereが述べたように、次の2つの方法上記とは別に

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

としてあなたwebpack.config.jsの上でそれを必要とすることができます

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.

、またpath.resolveを使用することができます。

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

は、それが役立ちます:)

+0

が魅力のように働きました!ありがとう@hardik .. –

+0

何かの理由で、私はまだパスで同じ例外が発生しています: "./dist/js"、__dirname + "dist/js"はうまくいきます。 –

+0

'。/ dist/js'は相対パスです。あなたが間違いを起こしている理由があるかもしれません。しかし、私のプロジェクトではうまくいっています。どのバージョンのWebpackを使用していますか? –

-1

あなたは絶対パスを取得するには、次のコードのようにそれを使用することができます願っています。あなたは次の操作を行い、あなたのパスに、その後の上部またはwebpack.config.jsファイルvar path = require('path') でこれを含めるとする必要が

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

はパスには必要ありません。ファイルの先頭に定義する必要がありますので、他のパスで参照することができます。それは悪い習慣です。 –

0

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

関連する問題