0

My code is as below. The contents of the file is a simple "hello world" I have the hello.docx file in the same folder I am calling this mammoth function.

Error result: fatal Error: ENOENT: no such file or directory, open './hello.docx'

Any idea what I am doing wrong? I am using this in my express node route

mammoth.extractRawText({path: "./hello.docx"})
    .then(function(result){
        var text = result.value; // The raw text 
        console.log(text);
        // var messages = result.messages;
    })
    .done();
rio
  • 116
  • 15

2 Answers2

0

It seems clear by the error displayed that the directory or file cannot be read. How are you uploading your file? If you are uploading your docx file using multer, you need to provide a reference to the file uploaded inside the path or buffer option as such:

mammoth.extractRawText({buffer: variable_that_holds_the_file.buffer})
    .then(function(result){
        var text = result.value; // The raw text 
        console.log(text);

    })
    .done();

Else, you need to revise the path since it may not be correct. To test this, use __dirname and or __filename in your console.log inside multer to see where your path is.

rags2riches-prog
  • 1,663
  • 1
  • 10
  • 22
0

use below code it might help you. it worked for me.

const mammoth = require("mammoth");
const path = require("path");

let filePath = path.join(__dirname,'./sampledocxfile.docx');

mammoth.extractRawText({path: filePath})
    .then(function(result){
        var html = result.value; 
        var messages = result.messages; 
        console.log(html);

    })
    .done();
Arrow
  • 153
  • 1
  • 2
  • 12