2

Sorry for yet another post on this topic, but my head is spinning from reading through what seems like hundreds of other stack overflow posts / articles and I'm still struggling with the "Module not found: Error: Can't resolve 'fs'" error.

Background is I am trying to run JavaScript OCR using the Tesseract.js libraries and running into a few roadblocks. I'm working in Palantir Foundry so my access to configurations and knowledge of the background setup is limited by access. I do know we are running node, typescript, and webpack 4.46.0. From process of elimination based on features that should exist globally in node and don't in my setup- best I can tell is we are running between node v6 and v8. I don't have access to make changes to node server or webpack configuration. The things I can edit are package.json and tsconfig.json, and I can add packages from npm.

I initially tried running the Tesseract.js library but that stalled due to what I believe are issues related to our node version being outdated.

I've switched to just importing the tesseract.js-core library in a very simple test package to read text from images, published the package on npm and installed in Foundry. When compiling in Foundry I get stuck on this error:

ERROR in ./node_modules/tesseract.js-core/tesseract-core.js
Module not found: Error: Can't resolve 'fs' in '/scratch/job_directory/ri.jemma.main.job.00000004-a768-b479-8777-c7f527d6b225-278aba35-9de5-4bc1-acf3-7a286db52107/repo/functions-typescript/node_modules/tesseract.js-core'
    @ ./node_modules/tesseract.js-core/tesseract-core.js 11:72-85
    @ ./node_modules/tmn-ocr/tesser.js
    @ ./src/index.ts

I've read many posts about include/exclude in tsconfig and what gets compiled in node_modules, I've tried to exclude things from node_modules, adding fs: false to package.json, skipLibCheck: true to tsconfig.json, and various other combinations of settings, none of it has worked. Here are my current configurations.

package.json:

{
    "name": "my-functions",
    "version": "0.0.0",
    "description": "My useful functions",
    "main": "dist/index.js",
    "types": "dist/index.d.ts",
    "dependencies": {
        "@foundry/functions-api": "0.75.0",
        "@foundry/functions-typescript-runtime-internal-api": "0.62.0",
        "@foundry/functions-utils": "0.10.0",
        "tesseract.js": "4.0.6",
        "@types/node": "*",
        "tmn-ocr": "1.0.2"
    },
    "devDependencies": {
        "@foundry/functions-typescript-authoring-dev": "0.56.0",
        "@foundry/functions-typescript-ontology-code-generator": "0.75.0",
        "@foundry/functions-typescript-discovery": "0.66.0",
        "@foundry/functions-typescript-object-set-base": "1.59.0",
        "@foundry/functions-testing-lib": "0.50.0",
        "jest": "27.5.1",
        "ts-jest": "27.1.5"
    },
    "browser": {
        "fs": false,
        "os": false,
        "path": false
    },
    "scripts": {
        "build": "../gradlew build",
        "check": "../gradlew check",
        "test": "../gradlew test",
        "jest": "jest"
    },
    "license": "UNLICENSED"
}

tsconfig.json

{
    "compilerOptions": {
        "explainFiles": true,
        "declaration": true,
        "downlevelIteration": true,
        "module": "commonjs",
        "sourceMap": true,
        "moduleResolution": "node",
        "lib": [
            "esnext"
        ],
        "target": "es6",
        "outDir": "./dist",
        "removeComments": true,
        "preserveConstEnums": true,
        "experimentalDecorators": true,
        "strict": true,
        "skipLibCheck": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "types": [
            "node",
            "jest",
            "@foundry/functions-typescript-authoring-dev"
        ]
    },
    "include": [
        "*.ts",
        "**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "./node_modules/tesseract.js-core/tesseract-core.js"
    ]
}  

I have read a few posts on fs and issues between running client side vs server side. It is entirely possible I am missing something fundamental about how node runs, but what is interesting is I can run 'fs' just fine in typescript. The compiler just doesn't like that the tesseract.js-core library requires it. Ultimately this will all run from a browser, but I am not trying to pull any information from client filesystem, just paths on the server.

I am at a loss here, my next step was to fork tesseract.js-core and modify it to get around the fs requirement, but this is not ideal for long term maintenance. Do I have any other options here before I head down that path? Thank you for reading!

tessa
  • 734
  • 1
  • 8
  • 22
  • 1
    Is the code running in the browser? – evolutionxbox Jun 13 '23 at 12:37
  • @evolutionxbox yeah ultimately it will run in a browser, I read a few posts on this and suspected this might be my issue. What is very confusing to me is I tried importing fs in a typescript file and compiling that (also for use in browser), and it doesn't complain about that. I am new to node/npm so guessing I am just not connecting all the dots here yet, but why would this work for me in typescript but not in the tesseract core library? – tessa Jun 13 '23 at 13:14
  • Also I'm assuming the developers of tesseract.js have accounted for this so I will take a run through there and see if I can find anything – tessa Jun 13 '23 at 13:16
  • Tesseract doesn't have fs as a dependency? https://www.npmjs.com/package/tesseract.js?activeTab=dependencies – evolutionxbox Jun 13 '23 at 13:24
  • yep that's right, tesseract.js has a dependency on tesseract.js-core which is where the fs dependency is – tessa Jun 13 '23 at 13:35
  • Are you transpiling it using webpack or another bundler? Since it doesn't work if you put it directly on the page. – evolutionxbox Jun 13 '23 at 14:32
  • It looks like just Webpack, only other references I see in the logs are to Jest (js testing framework?) – tessa Jun 13 '23 at 15:19
  • You are not getting a TS error when you import fs because you've installed and referenced the types for nodejs – Aluan Haddad Jun 13 '23 at 15:19
  • This thread on webpack github has been helpful in understanding some of what is going on here - https://github.com/webpack/webpack/issues/8826. The author also created this package. Still interested in other feedback but this did get rid of the build error in my case https://github.com/sindresorhus/require-fool-webpack – tessa Jun 14 '23 at 13:45

0 Answers0