Error when trying to run example from README.md
reregaga opened this issue · 4 comments
I am trying to run the example from the mupdf-js's README.md, in NodeJS environment (i am using the file in.pdf
from disk), but I am getting an error:
My code:
import createMuPdf from "mupdf-js";
import fs from "fs";
async function handleSomePdf(file) {
const mupdf = await createMuPdf();
//const buf = await file.arrayBuffer();
const arrayBuf = new Uint8Array(file); //buf);
const doc = mupdf.load(arrayBuf);
}
handleSomePdf(fs.readFileSync('in.pdf'));
Error:
D:\src> npm start
> src@1.0.0 start
> node index.js
file:///D:/src/index.js:5
const mupdf = await createMuPdf();
^
TypeError: createMuPdf is not a function
at handleSomePdf (file:///D:/src/index.js:5:23)
at file:///D:/src/index.js:11:1
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
at async loadESM (node:internal/process/esm_loader:85:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
Node.js v18.0.0
How can this be fixed?
Not enough information provided to be able to answer this. @reregaga I can't tell if you have mupdf-js
installed and available on the npm search path for your module.
Closing as this is an old issue.
@andytango , I had mupdf-js installed, so what i tried:
cd testfolder
npm init -y
npm i mupdf-js
My package.json
looks like:
{
"name": "testfolder",
"version": "1.0.0",
"main": "index.js",
"scripts": { "start": "node ." },
"dependencies": { "mupdf-js": "^1.1.1" }
}
Then I created file index.js
with content from readme's example and my addition lines for work with local pdf files:
import { createMuPdf } from "mupdf-js";
import fs from "fs";
async function handleSomePdf(file) {
const mupdf = await createMuPdf();
const buf = await file.arrayBuffer();
const arrayBuf = new Uint8Array(buf);
const doc = mupdf.load(arrayBuf);
const png = mupdf.drawPageAsPNG(doc, 1, 300);
const svg = mupdf.drawPageAsSVG(doc, 1);
const html = mupdf.drawPageAsHTML(doc, 1);
}
handleSomePdf(fs.readFileSync('in.pdf'));
Then I tried npm start
and got error:
Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
D:\testfolder\index.js:1
import { createMuPdf } from "mupdf-js";
^^^^^^
SyntaxError: Cannot use import statement outside a module
Then I tried rename index.js
to index.mjs
, but it gave me another error:
file:///D:/testfolder/index.mjs:5
const mupdf = await createMuPdf();
^
TypeError: createMuPdf is not a function
at handleSomePdf (file:///D:/testfolder/index.mjs:5:23)
at file:///D:/testfolder/index.mjs:19:1
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
at async loadESM (node:internal/process/esm_loader:85:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
Node.js v18.0.0
I can't figure out how to fix this. Can you please give a more detailed example?
Hey @reregaga ,
Thanks for the feedback - looks like I need to update the documentation 🙂
The examples on the readme are generally for browser environments as they make use of ArrayBuffers, which aren't needed on Node.
As for the specific issue you're facing, this is because NodeJS by default uses commonJS modules instead of ESM, unless told otherwise. You have two options to resolve this:
- Use
require
instead ofimport
:
const { createMuPdf } = require("mupdf-js");
const fs = require("fs");
async function handleSomePdf(file) {
// ...
}
- Change your package.json
type
tomodule
:
{
"name": "testfolder",
"version": "1.0.0",
"type": "module", <-------- HERE
"main": "index.js",
"scripts": { "start": "node ." },
"dependencies": { "mupdf-js": "^1.1.1" }
}
The second solution is considered to be more "modern" approach, as the community is transitioning away from commonJS. However you might find that other, older NPM packages don't work in your project, so you will need to use your judgement.
Once you do this, the example still won't work because you need don't need to use ArrayBuffers in the NodeJS environment.
You can fix this by changing the example to this:
import { createMuPdf } from "mupdf-js";
import fs from "fs";
async function handleSomePdf(file) {
const mupdf = await createMuPdf();
const doc = mupdf.load(file);
const png = mupdf.drawPageAsPNG(doc, 1, 300);
const svg = mupdf.drawPageAsSVG(doc, 1);
const html = mupdf.drawPageAsHTML(doc, 1);
}
handleSomePdf(fs.readFileSync('in.pdf'));
Hope all that helps!
Thanks for the detailed explanation. This example works great!