Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require is not defined javascript

****** EDIT *******

I do realize the mistake now, i was running my script in my terminal when i was testing my require.

****** OLD ****

The answer might all ready be here on the page, but i have not been able to find it, the general answers i see is that i cannot use "require" client sided.

The thing is the script i am trying to run works perfectly on my school laptop. But it doesnt want to run on my on Desktop at home.

I am trying to run a script where i want to require like this. "var fs = require('fs');"

But the console in my browser just gives me this error ( ReferenceError: require is not )

I am using Visual Studio Code, with Node.js.

I have a basic index.html file which uses 1 script.js file. Nothing else is being used for it.

A link for my files should you want to take a look ( https://drive.google.com/file/d/1VgEmwtcHkURFT1WmPOnEKr_OHLT_SY78/view?usp=sharing )

like image 900
JakeTheDane Avatar asked Jan 05 '18 13:01

JakeTheDane


People also ask

How do you fix require is not defined in JavaScript?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

Why require is not working in js?

This usually happens because your JavaScript environment doesn't understand how to handle the call to require() function you defined in your code. Here are some known causes for this error: Using require() in a browser without RequireJS. Using require() in Node.

How do you define require in js?

1) require()require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.

How do you fix the error require is not defined in NodeJS?

You are using require in a Node. If that is set to module , ES6 modules will be enabled and you will run into the error mentioned above (specifically ReferenceError: require is not defined in ES module scope, you can use import instead ). Simply remove that entry and you will be able to use require .


1 Answers

I am using Visual Studio Code, with Node.js.

I have a basic index.html file which uses 1 script.js file.

So you have two sets of JS.

  1. JS that runs in Node.js
  2. JS that runs in the browser (delivered via index.html)

var fs = require('fs'); is very much in the former category. require is a built-in in Node.js. fs is a core library of Node.js.

You are trying to run it in the browser, where it isn't supported and does not work. You need to run it in Node.js.

i see is that i cannot use "require" client sided.

There are browser-side implementations of require and tools (like Webpack) which will bundle modules into a browser compatible file, but neither of those approaches will help you here because fs itself depends on Node.js.

like image 106
Quentin Avatar answered Sep 24 '22 06:09

Quentin