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

Expo Android模拟器运行时sharp库依赖Node核心模块报错求助

问题:Expo Go运行应用时sharp依赖引发Node核心模块缺失错误

报错详情

终端运行时提示:

The package at "node_modules/sharp/lib/utility.js" attempted to import the Node standard library module "fs".
It failed because the native React runtime does not include the Node standard library.

调试expo/webpack时出现多个模块未找到错误:

ERROR in ./node_modules/sharp/lib/libvips.js:3
Module not found: Can't resolve 'fs'
  1 | 'use strict';
  2 |
> 3 | const fs = require('fs');
  4 | const os = require('os');
  5 | const path = require('path');
  6 | const spawnSync = require('child_process').spawnSync;

ERROR in ./node_modules/sharp/lib/libvips.js:6
Module not found: Can't resolve 'child_process'
  4 | const os = require('os');
  5 | const path = require('path');
> 6 | const spawnSync = require('child_process').spawnSync;
  7 | const semverCoerce = require('semver/functions/coerce');
  8 | const semverGreaterThanOrEqualTo = require('semver/functions/gte');
  9 |

ERROR in ./node_modules/sharp/lib/utility.js:3
Module not found: Can't resolve 'fs'
  1 | 'use strict';
  2 |
> 3 | const fs = require('fs');
  4 | const path = require('path');
  5 | const events = require('events');
  6 | const detectLibc = require('detect-libc');

ERROR in ./node_modules/sharp/node_modules/detect-libc/lib/detect-libc.js:6
Module not found: Can't resolve 'child_process'
  4 | 'use strict';
  5 |
> 6 | const childProcess = require('child_process');
  7 | const { isLinux, getReport } = require('./process');
  8 | const { LDD_PATH, readFile, readFileSync } = require('./filesystem');
  9 |

ERROR in ./node_modules/sharp/node_modules/detect-libc/lib/filesystem.js:6
Module not found: Can't resolve 'fs'
  4 | 'use strict';
  5 |
> 6 | const fs = require('fs');
  7 |
  8 | /**
  9 |  * The path where we can find the ldd

web compiled with 5 errors

已尝试的解决方法

  • 安装fs和child_process依赖:
    npm i fs
    npm i child_process
    
  • 在package.json中添加浏览器字段配置:
    "browser": {
        "fs": false,
        "os": false,
        "path": false
      }
    

解决方案

sharp是仅适用于Node.js环境的图片处理库,完全不支持Expo/React Native客户端运行环境(客户端没有fs、child_process等Node核心模块),核心解决思路是移除sharp并替换为客户端兼容方案:

  1. 直接移除sharp依赖
    执行命令卸载sharp,确保它不在项目依赖中:

    npm uninstall sharp
    

    检查package.json的dependencies和devDependencies字段,确认sharp已被移除。

  2. 替换客户端图片处理方案
    如果项目需要在客户端处理图片,改用Expo官方支持的库:

    • expo-image-manipulator:支持裁剪、缩放、旋转图片
    • expo-image-picker:用于选择或拍摄图片
    • expo-image:高性能图片展示组件
  3. 排查间接依赖
    如果是第三方包间接引入了sharp,用以下命令查找依赖链:

    npm ls sharp
    

    找到后,要么替换该第三方包,要么调整配置忽略sharp的引入(但更推荐替换,因为sharp在客户端无法正常工作)。

  4. 临时绕过错误(不推荐长期使用)
    若暂时无法移除sharp,可修改Expo的Webpack配置忽略相关模块:
    创建webpack.config.js文件,添加以下内容:

    const createExpoWebpackConfigAsync = require('@expo/webpack-config');
    
    module.exports = async function (env, argv) {
      const config = await createExpoWebpackConfigAsync(env, argv);
      config.resolve.fallback = {
        ...config.resolve.fallback,
        fs: false,
        child_process: false,
        os: false,
        path: false
      };
      return config;
    };
    

    注意:此方法仅能绕过报错,sharp的功能在客户端仍然无法运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 15:20:55