Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: can't find bundle.js

I've finally got the he dev server running and I get something on screen. I've setup a "start" script for NPM like this:

"start": "webpack-dev-server --content-base app"

I get an error:

http://localhost:8080/bundle.js Failed to load resource: the server responded with a status of 404 (Not Found)

My folders are set as follows:

appDir
  ->app
  ->node_modules
  webpack.config.js
  package.json

My webpack.config.js:

module.exports = {
    context: __dirname + '/app',
    entry: './index.js',
    output: {
        path: __dirname + '/app',
        filename: './bundle.js'
    }
}

Can you tell what's wrong?

like image 824
yccteam Avatar asked Apr 28 '15 15:04

yccteam


People also ask

How do I bundle a JavaScript file in webpack?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.

What is bundle JS webpack?

Webpack is an aggressive and powerful module bundler for JavaScript applications. It packages all the modules in your application into one or more bundles (often, just one) and serves it to the browser.

Does webpack need Index JS?

Out of the box, webpack won't require you to use a configuration file. However, it will assume the entry point of your project is src/index.js and will output the result in dist/main.js minified and optimized for production.

Does webpack bundle HTML?

js in the console! This means Webpack has successfully bundled both our logic from src/index. js and HTML from src/index. html into the dist directory, even automatically linking them together for us.


2 Answers

bundle.js is located inside your /app directory. That path option in output specifies the absolute path that the file goes.

Also you don't need the ./ in filename. It will get resolved relatively to output.path but it is confusing and may have contributed to your problem.

like image 124
Lim H. Avatar answered Oct 10 '22 05:10

Lim H.


The problem mostly with pointing bundle js in index.html. The reason webpack bundle.js is not found because you need to specify absolute path in index.html. Say suppose your bundle.js and index.html is generated under dist folder, then it should be something like below.

<script src="/bundle.js"></script>
like image 25
Hemadri Dasari Avatar answered Oct 10 '22 04:10

Hemadri Dasari