You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Webpack打包含Socket.IO项目时遇fs及sass-loader报错求助

Fixing Webpack Issues with Socket.IO, fs Module, and UglifyJs

Alright, let's break down your problems step by step and fix them properly—they stem from mismatched Webpack environment settings and outdated compression tooling.

First: Fix the "Can't resolve 'fs'" Error (Without Breaking Browser Builds)

Your initial fix of adding target: 'node' works for Node.js environments, but it's likely causing the later UglifyJs/sass-loader issues because you're probably building for the browser (not a Node backend). Here's the correct approach for browser-side builds:

  • Keep Webpack's default target: 'web' (this is what you want for browser code)
  • Only add the node: { fs: 'empty' } config to tell Webpack to stub the fs module (since Socket.IO references it but doesn't need it in the browser):
// webpack.config.js
module.exports = {
  // ... your other configs (entry, output, module rules, etc.)
  node: {
    fs: 'empty'
  }
};

This avoids pulling in Node-specific modules that clash with browser-focused tools like sass-loader.

Second: Fix the UglifyJs "Unexpected token: name (zlibLimiter)" Error

This error happens because the old uglifyjs-webpack-plugin doesn't support ES6+ syntax, and the zlibLimiter code (from Socket.IO) uses modern JS features. You have two solid fixes:

Terser is the replacement for UglifyJs and handles modern JS out of the box. Here's how to set it up:

  1. Install the plugin:
npm install terser-webpack-plugin --save-dev
  1. Update your Webpack config to use Terser instead of UglifyJs:
// webpack.config.js
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  // ... other configs
  optimization: {
    minimizer: [new TerserPlugin()]
  }
};

Option 2: Transpile All Code to ES5 (If You Must Use UglifyJs)

If you need to stick with UglifyJs, you'll need to make sure all code (including Socket.IO's modules) gets transpiled to ES5 via Babel:

  1. Update your Babel config to include Socket.IO's modules:
// .babelrc or babel.config.json
{
  "presets": ["@babel/preset-env"],
  "include": [
    "./src/**/*",
    "./node_modules/socket.io/**/*",
    "./node_modules/socket.io-client/**/*"
  ]
}
  1. Adjust your Webpack Babel loader to not exclude these modules:
// webpack.config.js
module.exports = {
  // ... other configs
  module: {
    rules: [
      {
        test: /\.js$/,
        // Only exclude node_modules that aren't Socket.IO related
        exclude: /node_modules(?!\/socket.io|\/socket.io-client)/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  }
};

Quick Check: Are You Building for Node.js or the Browser?

If you are building a Node.js backend (not browser code), then target: 'node' is correct—but the sass-loader error means you've accidentally included frontend Sass processing in your backend Webpack config. Just remove any sass-loader-related rules from that config, since Node backend code doesn't need to compile Sass.

内容的提问来源于stack exchange,提问作者harish511

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:14:48