20

I have a javascript in which I try to read a file and just print it on the console , however it gives "File is not defined" error inspite of the file test.txt being in the same path Following is the code snippet.

var txtFile = "test.txt";
var file = new File(txtFile);
file.open("r");
var str = "";
while (!file.eof) {
    str += file.readln() + "\n";
}
console.log(str);
file.close();    
3
  • 1
    Maybe : npmjs.com/package/file-class. Commented Jan 13, 2018 at 7:19
  • 2
    It is because the variable File is not defined. So you are trying to do new File(txtFile) but there is nothing that defines what File is. Commented Jan 13, 2018 at 7:26
  • NPM file-class doesn't seem to work if you are trying to pass an HTML File Element or Blob into a browser based function for unit testing. For one, there doesn't seem to be a file.size attribute. Commented Mar 3, 2022 at 23:51

2 Answers 2

7

This question was asked and answered in January 2018, when the latest version of node.js was v7.10.1, and that version didn't have the File API.

The ReferenceError you get is because there isn't a variable named File, and has nothing to do with the file you are trying to open.

You can test this by starting node interactively and enter File and press enter. Or try Life or any other word that isn't defined, and you get the error ReferenceError: <the identifier you wrote> is not defined.

The File API isn't available in Node version 7.10.1 or Edge prior to version 79. Most major browsers released support during 2014-2015.

Sign up to request clarification or add additional context in comments.

2 Comments

But this isn't a File type
@NewToCode What do you mean? The question was regarding the error "File is not defined", meaning that the variable "File" doesn't exist. In node the interface File doesn't exist. To read a file in node you can use the module fs after you have required or imported it. So what do you mean with your comment?
4

The Node command line environment does not implement the browser javascript's "File" class mainly due to the use-case scenarios. The browser File class was implemented for handling File inputs from form uploads.

The good news is the NodeJS project has recently merged a PR implementing support for the File class in the Node command line environment for compatibility purposes.

If you can't update to the latest NodeJS version to make use of this update, you can try using a polyfill library implementing File, however this necessitates an explicit import rather than File being implicitly available.

import {File} from '@web-std/file';

const file = new File(...

This may help you in the event your need for File is in first-party code rather than a third party library. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.