错误:找不到模块“socket.io-client/package"使用 webpack 构建时 [英] Error: Cannot find module "socket.io-client/package" when building with webpack

查看:76
本文介绍了错误:找不到模块“socket.io-client/package"使用 webpack 构建时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用socket.io构建简单的聊天服务器应用程序,并使用webpack将其组装成一个js文件.应用程序在没有 webpack 组装的情况下运行良好.但是当我尝试用 webpack 组装我的所有脚本时,它会抛出错误:

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"

我在 socket.io 库中找到了这个需要方法调用的地方.它在 socket.io/lib/index.js 文件中的第 11 行.

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;

因为我不明白 socket.io 需要它的依赖 socket.io-client 库的 package.json.我尝试添加一个 json 加载器,但没有帮助.我不知道接下来要做什么.

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

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

推荐答案

你需要 json-加载器.安装它.

You'll need json-loader. Install it.

我认为您还需要告诉 webpack 查找 json 文件.您可以使用 resolve.extensions 选项来做到这一点.只需在其中包含 '.json' 即可.

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.

我建议的一个更好的解决方案是使用 webpack 替换插件 并用实际版本替换该字符串.像这样:

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)

这篇关于错误:找不到模块“socket.io-client/package"使用 webpack 构建时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆