The package at "node_modules/sharp/lib/utility.js" attempted to import the Node standard library module "fs"

203 Views Asked by At

as i was trying to run my expo app in android studio virtual device. in expo go. i encountered a error in my terminal saying:

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.

and when i try to debug it expo/webpack. this is showing up.

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

i try to fix this by installing: fs, child_process using npm but it still there.

npm i fs
npm i child_process

then i try to follow some possible fix and it still there. like adding this to package.json. its not fixed still

"browser": {
    "fs": false,
    "os": false,
    "path": false
  }

this is my repo. if you wanna checkout the source code. https://github.com/jamilharun/CampusBytesMobile/tree/error

1

There are 1 best solutions below

0
Alisher On

sharp-cli is a CLI tool, you can't use it directly in your React Native code because it relies on fs package which is not present in the Hermes JS engine.

First, you need to remove this line from LoginScreen.js:

import constants from "sharp-cli/lib/constants";

Then reinstall sharp-cli as a development dependency (if you need this package at all):

npm install --save-dev sharp-cli