Reputation: 149
I am trying to build a react project with webpack but bundle.js is not created due to some error or issue in webpack.config.js.The app directory is as follows.
App Directory Structure
=>public
--scripts
--client
--client.js
--component
--App.js
--index.html
=>server
--server.js
=>webpack.config.js
Now , Here is code:
webpack.config.js
var webpack=require('webpack');
var pathresolver=require('path');
var BUILD_DIR=pathresolver.resolve(__dirname,'./public/scripts/client');
var APP_DIR=pathresolver.resolve(__dirname,'./public/dist');
module.exports={
devtool:'inline-source-maps',
entry: [
'webpack-hot-middleware/client',
BUILD_DIR+'/client.js'
],
output:{
path:APP_DIR,
filename:'bundle.js',
publicPath:'/'
},
plugins:[
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module:{
loaders:[
{
test:/\.js$/,
loader:'babel-loader',
exclude:/node_modules/,
query:{
presets:['react','es2015','react-hmre']
}
}
]
}
}
Server.js
var express= require('express');
var path=require('path');
var app=express();
//Get webapck details
var webpackconfig=require('../webpack.config.js');
var webpack=require('webpack');
var webpackDevMiddleware=require('webpack-dev-middleware');
var webpackHotMiddleware=require('webpack-hot-middleware');
var webpackcompiler=webpack(webpackconfig);
//Use middlewares
app.use(webpackDevMiddleware(webpackcompiler,{
noInfo:true,
publicPath:webpackconfig.output.publicPath
}));
app.use(webpackHotMiddleware(webpackcompiler));
app.use(express.static('../public'));
var filename = path.resolve(__dirname,'..','public','index.html');
app.use('/', function (req, res) {
res.sendFile(filename);
});
var port=3000;
app.listen(port,function(error){
if(error) throw err;
console.log("Server is running at port no "+port);
})
Component/App.js
import React,{Component} from 'react';
import {render} from 'react-dom';
class App extends Component{
render(){
return(
<div>Hello My name is Jalak Vora</div>
)
}
}
export default App
Client.js
import React from 'react';
import {render} from 'react-dom';
import App from '../component/App.js';
render(<App/>,document.getElementById(
"app")
)
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Sound Wave</title>
</head>
<body>
<div id="app"></div>
<script type="text/babel" src="dist/bundle.js"></script>
</body>
</html>
I am naive but i think the issue is webpack not able to find proper directory.Let me know what can be done.
Upvotes: 0
Views: 598
Reputation: 5098
I think it is due to some missing key-value pairs in scripts object of package.json file.
Upvotes: 0
Reputation: 255
I'm new to the webpack/React setup aswell. However; if you are trying to setup a react environment. I suggest following the first 10 minutes of the following video,it will help you setup your environment, but from my understanding the process does not actually create a bundle.js file and somehow it does it within webpack configuration. Good luck
https://www.youtube.com/watch?v=IR6smI_YJDE&index=1&list=PLbeWHDJFd79RXvAeGaDXMmxL_tkb3MvdH
Upvotes: 0