简体   繁体   中英

Missing output file from webpack and node.js

I'm trying to setup a node.js server. Currently when I run npm start with the current webpack.config.js everything loads. When I run node server, the index.js is missing.

webpack.config.js
   var config = {
   context: path.join(__dirname, 'src'),
   entry: './js/main.js',
   output: {
      path: __dirname + '/src/',
      filename: 'index.js',
   },
   devServer: {
      inline: true,
      port: 8080
   },
   module: {
      loaders: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;

Here is the html, left out things not needed to see.

<html lang="en">

  <head>
  </head>

  <body>
    <div id="app"></div>
    <script src="index.js"></script>
  </body>

</html>

Package.json

{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "start": "webpack-dev-server --content-base src --hot",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "history": "^1.17.0",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-router": "^4.2.0",
    "webpack": "^3.8.1",
    "webpack-dev-server": "^2.9.3"
  },
  "devDependencies": {
    "express": "^4.16.2",
    "http": "0.0.0",
    "react-socket-io": "^0.2.4",
    "socket.io": "^2.0.3",
    "webpack": "^3.8.1"
  }
}

I'm fairly new to node.js and webpack so I'm not exactly sure why index.js is getting outputted when I run npm start but not when I run node server. I can upload the server.js but it follows a very basic template.

Edit: It seems I got it working, I need to run webpack dev server as well as the node.js server on different ports. Follow up question, is it possible to change this to only run the node server and still have everything work?

You need to run webpack --config path/to/webpack.config.js to generate index.js in the file system before starting node server.

You can add build command to your package.json :

{
  ...
  "scripts": {
    "start": "webpack-dev-server --content-base src --hot",
    "build": "webpack --config webpack.config.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  ...
}

and then before starting node server run npm run build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM