Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack dev server not able to find node_modules

I am new to webpack, I am facing few problems while trying to setup webpack.

I have the following directory structure:

  • Node Modules
  • Public (index.html, index.jsx)
  • Components
  • webpack.config.js

In index.html I have tried to include

<script src="../node_modules/react/dist/react-with-addons.js"></script>

and when I am trying to run webpack dev server console is showing me

http://localhost:8080/node_modules/react/dist/react-with-addons.js not found

The following is my webpack.config.js file:

module.exports = {
    //This is the entry point for the webpack
    entry: {
    app: ['./public/index.jsx']
    },
    output: {
    // This is the name of the bundle which is created  when webpack runs
    path: './public',
    filename: 'bundle.js' 
    },
    module: {
        loaders: [
            {
                //tell webpack to use jsx-loader for all *.jsx files
                test: /\.jsx$/,
                loader: 'jsx-loader?insertPragma=React.DOM&harmony'
            }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    }
}
like image 545
Vamshi Kolanu Avatar asked Jul 18 '15 16:07

Vamshi Kolanu


1 Answers

I know this is quite an old question, but I struggled with this today.

A solution I'm using is passing an array to the contentBase with node_modules.

devServer: {
    contentBase: [
      path.resolve(__dirname, "public"),
      path.resolve(__dirname, "node_modules")
    ],
    publicPath:  "/"
}

Then in your html:

<script src="./react/dist/react.js"></script>

This way you don't need to include React in your bundle and it can be cached by the browser.

like image 128
Michelangelo Partipilo Avatar answered Nov 10 '22 16:11

Michelangelo Partipilo