Нужно собрать вот это:
//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)
Мой конфиг:
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"
}];
После сборки, которая проходит без ошибок, при попытке запустить сервер получаю:
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)
Я пытался решить проблему при помощи псевдонимов в вебпаке, как видно - не помогло.