the_islander
the_islander

Reputation: 207

bundle.js not found with webpack

Learning to use webpack to set up a MERN stack project. After running webpack to bundle everything and starting the express server I see that bundle.js is not found and I see a localhost:3000/bundle.js 404 status code in the console. Maybe my paths are incorrect or I'm missing something

Package.json

{
  "name": "mern_tut",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "express": "^4.16.1",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "webpack": "^3.6.0"
  }
}

webpack.config.js

const path = require('path');
module.exports = {
  entry: './static/app.js',
  output: {
    path: path.resolve('dist'),
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },


    ]

  }
}

.babelrc

{
    "presets":[
        "es2015", "react"
    ]
}

server.js

var express = require('express')

var app = express();

app.use(express.static('static'));

var server = app.listen(3000, function() {
    var port = server.address().port;
    console.log("Started server at port", port);
});

project setup

 - dist
    -bundle.js
- node_modules
- static
   -app.js
   -index.html
- .babelrc
- package.json
- server.js
- webpack.config.js

Upvotes: 3

Views: 2077

Answers (2)

Hemadri Dasari
Hemadri Dasari

Reputation: 33974

Here is my solution which I am using in my project

// server.js
const express = require('express')
const path = require('path')

const app = express()

// serve our static stuff like index.css
app.use(express.static(path.join(__dirname, 'dist')))

// send all requests to index.html so browserHistory in React Router works
app.get('*', function (req, res) {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'))
})

const port = process.env.PORT || 3000
app.listen(port,() => {
   console.log(`App started on port: ${port}`);
});

Upvotes: 0

user-developer
user-developer

Reputation: 586

You are not serving your bundle.js here. Add following to your server.js

app.use(express.static('dist')) 

Upvotes: 2

Related Questions