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

Node.js项目中require("jpgJs")返回空对象{}的问题求助

Fixing jpgjs Export Issue for Utif in React/Node.js Projects

Root Cause

I’ve run into this exact issue before—here’s what’s going wrong: jpgjs’s export logic in jpg.js line 102 has a bug for Node.js environments. The condition if (typeof exports === "function") is incorrect because in Node.js, exports is always an object, not a function. That branch never triggers, so require("jpgjs") returns an empty {} instead of exposing the JpegDecoder that Utif relies on.

Solutions

Here are three solid ways to fix this, depending on your project needs:

1. Quick Manual Patch (For Testing/One-Off Projects)

  • Head to node_modules/jpgjs/jpg.js in your project folder
  • Find line 102 and replace the faulty condition:
    // Original code
    if (typeof exports === "function")
    // Replace with this to target Node.js exports
    if (typeof exports !== "undefined")
    
  • Save the file. Now require("jpgjs") will correctly return the JpegDecoder Utif needs to work.

2. Create a Wrapper Module (Cleaner for Long-Term Use)

Avoid modifying node_modules directly by making a wrapper file (e.g., src/utils/jpgjs-fix.js):

// Load the raw jpgjs source (it attaches JpegDecoder to the global scope)
require('jpgjs/jpg.js');

// Export the decoder for your project to use
module.exports = global.JpegDecoder;

Then in your code, import from this wrapper instead of the raw jpgjs package:

const JpegDecoder = require('./utils/jpgjs-fix.js');
// Pass this to Utif or use it directly as needed

3. Use patch-package (Best for Team/Shared Projects)

This ensures the fix sticks even after running npm install:

  1. Install patch-package as a dev dependency:
    npm install patch-package --save-dev
    
  2. Modify node_modules/jpgjs/jpg.js line 102 as described in solution 1
  3. Generate a persistent patch file:
    npx patch-package jpgjs
    
  4. Commit the generated patches/jpgjs+<version>.patch file to your repo. Now every time your team runs npm install, the patch will automatically apply.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:47:18