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!