webpack dev服务器无法找到node_modules

Vam*_*anu 10 reactjs webpack webpack-dev-server

我是webpack的新手,在尝试设置webpack时遇到的问题很少.

我有以下目录结构:

  • 节点模块
  • 公共(index.html,index.jsx)
  • 组件
  • webpack.config.js

在index.html中,我试图包含

<script src="../node_modules/react/dist/react-with-addons.js"></script>
Run Code Online (Sandbox Code Playgroud)

当我试图运行webpack时,dev服务器控制台正在向我展示

http://localhost:8080/node_modules/react/dist/react-with-addons.js not found
Run Code Online (Sandbox Code Playgroud)

以下是我的webpack.config.js文件:

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']
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 10

我知道这是一个很老的问题,但今天我一直在努力.

我正在使用的解决方案是将数组传递给contentBasewith node_modules.

devServer: {
    contentBase: [
      path.resolve(__dirname, "public"),
      path.resolve(__dirname, "node_modules")
    ],
    publicPath:  "/"
}
Run Code Online (Sandbox Code Playgroud)

然后在你的HTML中:

<script src="./react/dist/react.js"></script>
Run Code Online (Sandbox Code Playgroud)

这样您就不需要在捆绑包中包含React,它可以由浏览器缓存.