Error: Cannot find module “socket.io-client/packag

2019-07-07 05:52发布

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' }
        ]
    }
}

1条回答
何必那么认真
2楼-- · 2019-07-07 06:11

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:

new webpack.NormalModuleReplacementPlugin('require(\'socket.io-client/package\').version', require('socket.io-client/package').version)
查看更多
登录 后发表回答