165

Basically, I am using javascript to scrape data from Google Play store using:

1-Request

2-Cheerios

3-QueryString

I used Google Market API from Github which uses require as following:

var request = require('request');
var cheerio = require('cheerio');
var qs = require('querystring');

But I am getting the following

Uncaught ReferenceError: require is not defined ...

So, I don't have to require() in javascript which is either new for me or this is something out of the ordinary.

2

6 Answers 6

120

RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.

IE 6+ .......... compatible ✔
Firefox 2+ ..... compatible ✔
Safari 3.2+ .... compatible ✔
Chrome 3+ ...... compatible ✔
Opera 10+ ...... compatible ✔

http://requirejs.org/docs/download.html

Add this to your project: https://requirejs.org/docs/release/2.3.5/minified/require.js

and take a look at this http://requirejs.org/docs/api.html

1
  • 1
    I added this script and still get "require is not defined". Commented May 24, 2023 at 13:27
48

By default require() is not a valid function in client side javascript. I recommend you look into require.js as this does extend the client side to provide you with that function.

14

Require (https://requirejs.org/) is an AMD API . I had a similar issue while implementing monaco-editor in my application. This script tag helped me:

<script src="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script>
1
  • By the way to use monaco editor you don't need the require tag since currently it is there in the monaco cdn itself Commented Jul 2, 2021 at 8:01
8

Yes, require is a Node.JS function and doesn't work in client side scripting without certain requirements. If you're getting this error while writing electronJS code, try the following:

In your BrowserWindow declaration, add the following webPreferences field: i.e, instead of plain mainWindow = new BrowserWindow(), write

mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true
        }
    });
2
  • 4
    still getting same issue Commented Jul 26, 2021 at 16:31
  • IN which place i need to write this code ? mainWindow = new BrowserWindow({ webPreferences: { nodeIntegration: true } }); Commented Sep 18, 2023 at 11:00
4

Browsers don't have the require method defined, but Node.js does. With Browserify you can write code that uses require in the same way that you would use it in Node.

Use the following command to install browserify

npm install -g browserify

Now recursively bundle up all the required modules like main.js into a single file called bundle.js with the browserify command:

browserify main.js -o bundle.js

Drop a single tag into your html and you're done!

<script src="bundle.js"></script>

For more details click here https://www.npmjs.com/package/browserify and https://browserify.org/#install

1
  • it says The term 'browserify' is not recognized as the name of a cmdlet when doing "browserify main.js -o bundle.js"
    – ashrth
    Commented Dec 19, 2022 at 11:15
2

For me the issue was I did not have my webpack build mode set to production for the package I was referencing in. Explicitly setting it to "build": "webpack --mode production" fixed the issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.