Mindee API throws error- Error: Cannot find module 'node:buffer' - javascript

I was exploring Mindee to make a code that converts the pdf to text, so I followed the steps and used the code from the documentation, but when I run it with node it throws this error:
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'node:buffer'
Require stack:
- C:\Users\budgi\OneDrive\Documentos\TrabajosBi\Proy\Fedex\v2\node_modules\mindee\src\inputs\sources.js
- C:\Users\budgi\OneDrive\Documentos\TrabajosBi\Proy\Fedex\v2\node_modules\mindee\src\inputs\index.js
- C:\Users\budgi\OneDrive\Documentos\TrabajosBi\Proy\Fedex\v2\node_modules\mindee\src\client.js
- C:\Users\budgi\OneDrive\Documentos\TrabajosBi\Proy\Fedex\v2\node_modules\mindee\src\index.js
- C:\Users\budgi\OneDrive\Documentos\TrabajosBi\Proy\Fedex\v2\app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\budgi\OneDrive\Documentos\TrabajosBi\Proy\Fedex\v2\node_modules\mindee\src\inputs\sources.js:30:23)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\budgi\\OneDrive\\Documentos\\TrabajosBi\\Proy\\Fedex\\v2\\node_modules\\mindee\\src\\inputs\\sources.js',
'C:\\Users\\budgi\\OneDrive\\Documentos\\TrabajosBi\\Proy\\Fedex\\v2\\node_modules\\mindee\\src\\inputs\\index.js',
'C:\\Users\\budgi\\OneDrive\\Documentos\\TrabajosBi\\Proy\\Fedex\\v2\\node_modules\\mindee\\src\\client.js',
'C:\\Users\\budgi\\OneDrive\\Documentos\\TrabajosBi\\Proy\\Fedex\\v2\\node_modules\\mindee\\src\\index.js',
'C:\\Users\\budgi\\OneDrive\\Documentos\\TrabajosBi\\Proy\\Fedex\\v2\\app.js'
]
}
These are the folders:
folders
And the app.js (pasted the code from the documentation)
const mindee = require("mindee");
// for TS or modules:
// import * as mindee from "mindee";
// Init a new client
const mindeeClient = new mindee.Client({apiKey: "1234567890"});
// Load a file from disk and parse it
const invoiceResponse = mindeeClient
.docFromPath("/path/to/the/invoice.pdf")
.parse(mindee.InvoiceV3);
// Print a the parsed data
invoiceResponse.then((resp) => {
// The document property can be undefined:
// * TypeScript will throw an error without this guard clause
// (or consider using '?' notation)
// * JavaScript will be very happy to produce subtle bugs
// without this guard clause
if (resp.document === undefined) return;
// full object
console.log(resp.document);
// string summary
console.log(resp.document.toString());
});
What can be the cause of the error?

Related

Update JSON file write not working in NodeJS

I'm trying to read existing JSON file and update the existing value and saving it back. I'm getting below error message:
node:internal/modules/cjs/loader:944
throw err;
^
Error: Cannot find module 'src/myApp/test.json'
Require stack:
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
at Function.Module._load (node:internal/modules/cjs/loader:774:27)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) {
code: 'MODULE_NOT_FOUND',
}
Please find the code below -
Much appreciated if someone can help me to fix this issue.
test.json
{
"key": ""
}
updateJSON.js
const fs = require('fs');
const fileName = 'src/myApp/test.json';
const file = require(fileName);
file.key = "new value";
fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
if (err) return console.log(err);
console.log(JSON.stringify(file));
console.log('writing to ' + fileName);
});
The error message Error: Cannot find module 'src/myApp/test.json' indicates that the Node.js cannot resolve the src/myApp/test.json file correctly.
By default, the name(src/myApp/test.json here) not starting with ./ will be recognized as a module of Node.js and will be resolved from native modules and node_modules, which leads to the error.
Using a relative path like ../src/myApp/test.json should fix it.
Ref: https://nodejs.org/api/modules.html#modules_require_id

html-pdf: Failed to load PhantomJS module. Error: Cannot find module 'phantomjs-prebuilt'

I am using html-pdf NPM module to convert my html (bill.html) code to pdf file (bill.pdf), I have simple html with heading tag and exporting that html by fs module below
import { create } from 'html-pdf';
import fs from 'fs';
import path from 'path';
var html = fs.readFileSync(path.resolve(__dirname, "./bill.html"), 'utf8');
var options = { format: 'Letter' };
create(html, options).toFile('./bill.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res);
});
And I am getting this error of 'phantomjs-prebuilt'
html-pdf: Failed to load PhantomJS module. Error: Cannot find module 'phantomjs-prebuilt'
Require stack:
- /home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js
- /home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js
- /home/hardy/Documents/personal/api/src/app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
at Function.Module._load (internal/modules/cjs/loader.js:686:27)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js:7:19)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Object.require.extensions.<computed> [as .js] (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:152:7)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js',
'/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js',
'/home/hardy/Documents/personal/api/src/app.js'
]
}
Debugger listening on ws://127.0.0.1:5858/508c0ea9-c495-4254-9b05-cd2fd3cd8ae3
For help, see: https://nodejs.org/en/docs/inspector
assert.js:374
throw err;
^
AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'
at new PDF (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js:38:3)
at createPdf (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js:10:14)
at Object.<anonymous> (/home/hardy/Documents/personal/api/src/app.js:8:1)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at loader (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.<computed> [as .js] (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at Object.<anonymous> (/home/hardy/Documents/personal/api/node_modules/babel-cli/lib/_babel-node.js:154:22) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: true,
operator: '=='
}
Waiting for the debugger to disconnect...
As phantomjs-rebuilt is a deprecated
You can still install it forcefully and resolve the error.
Command - npm i phantomjs-prebuilt --force

How to run javascript function from .js file via console?

I want to run some function inside javascript file via the console.
I'm using Node v12.14.1.
My case is as follows:
I have a testIt.js file as follows:
import {settings} from "./config";
const someMainFunction = (testParam) => {
console.log("This is init");
console.log("This PARAM", testParam);
otherFunction();
};
const otherFunction = () => {
console.log("This is other function")
};
export { someMainFunction }
I want to run someMainFunction via console.
I tried with
node -e 'require("./testIt").someMainFunction("Testing...")'
But it doesn't work.
I do not want to use any external libraries.
Any idea how to do that?
UPDATE
I get an error as follows:
import {settings} from "./frontend/config";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:895:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at [eval]:1:1
at Script.runInThisContext (vm.js:116:20)
at Object.runInThisContext (vm.js:306:38)
at Object.<anonymous> ([eval]-wrapper:9:26)
I've tried also with:
node --harmony testIt.js
But I get the same error.
UPDATE 2
When I try with
node --experimental-modules testIt.mjs
I get an error as follows:
(node:99449) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:84
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find module /Users/username/Projects/config imported from /Users/username/Projects/testIt.mjs
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:84:13)
at Loader.resolve (internal/modules/esm/loader.js:73:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:147:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:41:40)
at link (internal/modules/esm/module_job.js:40:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
UPDATE 3
import {settings} from "./frontend/config";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:895:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at [eval]:1:1
at Script.runInThisContext (vm.js:116:20)
at Object.runInThisContext (vm.js:306:38)
at Object.<anonymous> ([eval]-wrapper:9:26)

I cannot use import in Jasmine2 spec when using protractor 4.0.9 when using typescript 2.0

When I try to use typescript .ts spec and run them in protractor, I get:
[17:05:52] E/launcher - Error: SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:84:5
at Array.forEach (native)
at Jasmine.loadSpecs (C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:83:18)
[17:05:52] E/launcher - Process exited with error code 100
When I have .ts file and no import is present it's behaving ok. Why ?
) Get the same error, using: Jasmine2, Protractor 4.0.11, typescript 2.0.3
And I don't know what seems to be the problem. But my workaround is:
in file: main-page.ts
module.exports = {
setUrl: function(){
browser.get("/");
}
};
and in file you want to use the function setURL for example:
var mainPage = require('./main-page.ts');
describe('Test Main Page', function() {
it('should have a title', function() {
mainPage.setUrl();
expect(browser.getTitle()).toEqual('Titel meiner Seite');
});
});
That's just my workaround - actually I didn't find out the real reason for the problem you described!

Requiring other files in node

I'm trying to require another file within a node project I'm working on; this will be a command line tool. What I'm trying to do is create a formatted color output using the following file format.js:
modules.exports = {
warning: function(input){
say("\033[31m" + input)
},
info: function(input){
say("\033[36m" + input)
}
}
From there I want to create the colored output and put it into a file named gen_email.js. That file has these two functions in it:
function say(input){
console.log(input)
}
function helpPage(){
say('');
format.info("test")
}
When I attempt to run this it outputs the following:
C:\Users\thomas_j_perkins\bin\javascript\node\email\lib\format.js:1
(function (exports, require, module, __filename, __dirname) { modules.exports = {
^
ReferenceError: modules is not defined
at Object.<anonymous> (C:\Users\thomas_j_perkins\bin\javascript\node\email\lib\for
mat.js:1:63)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\thomas_j_perkins\bin\javascript\node\email\gen_ema
il.js:3:16)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
I'm not understanding what I'm doing wrong, according to this I am requiring the file the correct way. What am I doing wrong here, do I need to move the say function into the other file?
It should be
module.exports = {
warning: function(input){
say("\033[31m" + input)
},
info: function(input){
say("\033[36m" + input)
}
}
in the other file
const format = require("whatEverPathIsOn/format.js")
if the file is under the same path just
const format = require("./format.js")
That should be module, not modules.

Categories

Resources