require is not defined in node.js 14.4.0

385 views
Skip to first unread message

Matthew Hamilton

unread,
Jun 7, 2020, 12:34:00 PM6/7/20
to nodejs
node.js 14.4.0 macOS Catalina, Visual Studio Code debugging. Line 11 in my app.mjs file is var FileSystem = require('fs'); The debugger throws "require is not defined" error on this line. Comment this line out and the debugger throws an error on line 23 which is var PDFImage = require('pdf-image'). the pdf-image package is installed. I'm not sure what is going on. The only help I can find is about require not working in a browser. I AM NOT RUNNING IN A BROWSER. I AM RUNNING IN NODE.JS. Thank you!

John Shaver

unread,
Jun 7, 2020, 5:51:28 PM6/7/20
to nod...@googlegroups.com
In an .mjs file, you do not use require.  You use import.  Try: 

import fs from "fs";

More info:

-John

On Sun, Jun 7, 2020, 9:33 AM Matthew Hamilton <buddh...@gmail.com> wrote:
node.js 14.4.0 macOS Catalina, Visual Studio Code debugging. Line 11 in my app.mjs file is var FileSystem = require('fs'); The debugger throws "require is not defined" error on this line. Comment this line out and the debugger throws an error on line 23 which is var PDFImage = require('pdf-image'). the pdf-image package is installed. I'm not sure what is going on. The only help I can find is about require not working in a browser. I AM NOT RUNNING IN A BROWSER. I AM RUNNING IN NODE.JS. Thank you!

Reply all
Reply to author
Forward
0 new messages