Trying to build simple chat server application with socket.io, and assemble it to one js file with webpack. App works well without webpack assembling. But when I try to assemble all my scripts with webpack, it throws error:
Error: Cannot find module "socket.io-client/package"
I have find the place in socket.io library where this require method calls. Its 11 line in socket.io/lib/index.js file.
var clientVersion = require('socket.io-client/package').version;
As I undestand socket.io requires package.json of it`s dependency socket.io-client library. I tried to add a json loader but it did not help. What to do next I don't know.
main.js:
var io = require('socket.io');
var listener = io(9999);
...
webpack.config
var webpack = require('webpack');
module.exports = {
entry: './src/main.js',
target: 'node',
output: {
path: './temp',
filename: 'chat-server.js'
},
module: {
loaders: [
{ test: /\.json$/, loader: 'json' }
]
}
}
You'll need
json-loader
. Install it.I think you'll also need to tell webpack to look for json files. You can do that with the
resolve.extensions
option. Just include'.json'
there.A better solution I would suggest is to use a webpack replace plugin and replace that string with the actual version. Something like this: