B
B
B1dloKoder2018-12-15 12:43:47
Node.js
B1dloKoder, 2018-12-15 12:43:47

How to include modules when building webpack?

You need to collect this:

//const express = require('express')
//const app =  express();

import * as express from 'express';
import * as io from "socket.io";
import * as http from 'http';

const app = express();

app.use(express.static(__dirname + '/../static') );

//const http = require('http').Server(app);

//const io = require('socket.io')(http);

io.on('connection', function(socket){
    socket.on('login', (data) => {
        socket.login = data
        io.emit('login', data)
        
    })

    socket.on('sendMessage', (data) => {
        io.emit('takeMessage', {login: socket.login, text: data })
    })
  });

let port = process.env.PORT || 5000;

http.listen(port)

My config:
const path = require('path'); 

module.exports = [ { 

    entry: path.join(__dirname,'./src/index.js'), 

    output:{ 

        path: path.join(__dirname, '/app/js'), 

        filename: 'bundle.js' 

    }, 

    mode: "development", 

    externals: "ws",

    resolve: {
        alias: {
            'socket.io-client': path.join( __dirname, 'node_modules', 'socket.io-client', 'socket.io.js' )
        }
    },

    devServer: { historyApiFallback: true },


    module: { 
        rules: [ 

            { 

                test: /\.(js|jsx)$/, 

                use: [ 

                    { 

                        loader: 'babel-loader', 

                        options: { 

                            presets: ['env', 'react'] 

                        } 

                    } 

                ] 

            } 

        ],
        noParse: [ /socket.io-client/ ]

    }, 

    watch: true, 

    watchOptions: { 

        aggregateTimeout: 200 

    }, 

    target: 'node' 

}, 
{
    entry: path.join(__dirname,'./server/server.js'), 

    output:{ 

        path: path.join(__dirname, '/app/server'), 

        filename: 'server.js' 

    }, 
    module: {
        rules: [
          {
            test: /\.js$/,
            use: 'node-loader'
          }
        ]
    },
    resolve: {

        alias: {
          express: path.resolve(__dirname,  'node_modules', 'express'),
          io: path.resolve(__dirname,  'node_modules', 'socket.io'),
          http: path.resolve(__dirname,  'node_modules', 'http'), 
        }
      },

    mode: "development", 
    target: "node"
}];

After the build, which runs without errors, when I try to start the server, I get:
webpack:///(webpack)/buildin/module.js?:2
/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./module.js */ "./node_modules/webpack/buildin/module.js")(module)))
                                                                                                                          ^

TypeError: __webpack_require__(...) is not a function
    at eval (webpack:///(webpack)/buildin/module.js?:2:123)
    at Object../node_modules/webpack/buildin/module.js (/home/podnebesnyy/Рабочий стол/Чат/app/server/server.js:96:1)
    at __webpack_require__ (/home/podnebesnyy/Рабочий стол/Чат/app/server/server.js:20:30)
    at eval (webpack:///./server/server.js?:2:41)
    at Object../server/server.js (/home/podnebesnyy/Рабочий стол/Чат/app/server/server.js:107:1)
    at __webpack_require__ (/home/podnebesnyy/Рабочий стол/Чат/app/server/server.js:20:30)
    at /home/podnebesnyy/Рабочий стол/Чат/app/server/server.js:84:18
    at Object.<anonymous> (/home/podnebesnyy/Рабочий стол/Чат/app/server/server.js:87:10)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)

I tried to solve the problem using aliases in webpack, apparently - it did not help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question