1

每當我跑webpack-dev-server它拋出這個錯誤:無法配置webpack.config文件

....webpack/hot/only-dev-server ./src Module not found: Error: Can't resolve './src' in 'C:\....

這裏的webpack.config.js文件

var webpack = require('webpack'); 
var path = require('path'); 

module.exports = { 
    devtool: "inline-source-map", 
    entry: [ 
     "webpack-dev-server/client?http://127.0.0.1:8080/", 
     "webpack/hot/only-dev-server", 
     "./src" 
    ], 
    output: { 
     path: path.join(__dirname, 'public'), 
     filename: 'bundle.js' 
    }, 
    resolve: { 
     modules: ['node_modules', 'src'], 
     extensions: ['.js'] 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.jsx?$/, 
       exclude: /(node_modules|bower_components)/, 
       loaders: ['react-hot-loader','babel-core?presets[]=react,presets[]=es2015'], 
      } 
     ] 
    }, 
    plugins: [ 
     new webpack.HotModuleReplacementPlugin(), 
     new webpack.NoEmitOnErrorsPlugin() 
    ], 
}; 

的package.json

{ 
    "name": "react-todo", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "keywords": [], 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "babel": "^6.23.0", 
    "babel-core": "^6.23.1", 
    "babel-loader": "^6.3.2", 
    "babel-plugin-add-module-exports": "^0.2.1", 
    "babel-plugin-react-html-attrs": "^2.0.0", 
    "babel-plugin-transform-class-properties": "^6.23.0", 
    "babel-plugin-transform-decorators-legacy": "^1.3.4", 
    "babel-preset-es2015": "^6.22.0", 
    "babel-preset-react": "^6.23.0", 
    "babel-preset-stage-0": "^6.22.0", 
    "lodash": "^4.17.4", 
    "react": "^15.4.2", 
    "react-dom": "^15.4.2", 
    "webpack": "^2.2.1", 
    "webpack-dev-server": "^2.3.0" 
    }, 
    "devDependencies": { 
    "react-hot-loader": "^1.3.1" 
    } 
} 

我該如何解決?

+0

您的'src'文件夾中是否有'index。*'? src'文件夾是否存在?嘗試將最後一個入口點作爲文件而不是文件夾,例如'。/ src'到'。/ src/index.js' –

+0

現在它說'不能解決react-hot',更新我的代碼! –

+0

你是否'npm install --save-dev react-hot-loader'?現在我做了 –

回答

1

似乎你(錯誤消息'C:\...')使用Windows和的WebPack expects窗戶路徑分隔符,像\,不/。你可以試試".\src\your_entry_file.js"而不是"./src"

1

試試這個:

更新你的包JSON這樣:

{ 
    "name": "react-todo", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1", 
    "start": "node server.js" 
    }, 
    "keywords": [], 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "lodash": "^4.17.4", 
    "react": "^15.4.2", 
    "react-dom": "^15.4.2" 
    }, 
    "devDependencies": { 
    "react-hot-loader": "^1.3.1", 
    "webpack": "^2.2.1", 
    "webpack-dev-server": "^2.3.0", 
    "babel": "^6.23.0", 
    "babel-core": "^6.23.1", 
    "babel-loader": "^6.3.2", 
    "babel-plugin-add-module-exports": "^0.2.1", 
    "babel-plugin-react-html-attrs": "^2.0.0", 
    "babel-plugin-transform-class-properties": "^6.23.0", 
    "babel-plugin-transform-decorators-legacy": "^1.3.4", 
    "babel-preset-es2015": "^6.22.0", 
    "babel-preset-react": "^6.23.0", 
    "babel-preset-stage-0": "^6.22.0", 
    } 
} 

然後從你的項目文件夾的根目錄運行npm install

webpack.config.js這個

var webpack = require('webpack'); 
var path = require('path'); 

module.exports = { 
    devtool: "inline-source-map", 
    entry: [ 
     "webpack-dev-server/client?http://127.0.0.1:8080/", 
     "webpack/hot/only-dev-server", 
     "./src/app.js" // or whatever your entry file is 
    ], 
    output: { 
     path: path.join(__dirname, 'public'), 
     filename: 'bundle.js' 
    }, 
    resolve: { 
     modules: ['node_modules', 'src'], 
     extensions: ['.js'] 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.js$/, 
       exclude: /(node_modules|bower_components)/, 
       loader: 'babel-loader', 
       query: { 
        presets: ['es2015'] 
       } 
      }, 
      // the loader should be broken up into two objects 
      { test: /\.jsx?$/, loaders: ['react-hot', 'jsx?harmony'], include: path.join(__dirname, 'src') } 
     ] 
    }, 
    plugins: [ 
     new webpack.HotModuleReplacementPlugin(), 
     new webpack.NoEmitOnErrorsPlugin() 
    ], 
}; 
更新

下載thisserver.js文件並將其保存到項目文件夾。

然後嘗試npm start

希望工程。祝你好運。請參考here

+0

錯誤再次檢查討論 –

+0

@JaskaranSinghPuri哦,我忘記了,也做'npm install --save-dev express' –