6

I am trying to set up a basic contact form using SengGrid and I keep getting a "Uncaught ReferenceError: require is not defined" error.

I have this code in a script tag in the head of the html page.

    var sendgrid = require('sendgrid')(username,pass);

I have looked at requirejs, but I am not sure why I am getting this error. Could someone explain to me how I can resolve this issue?

wonea
  • 4,783
  • 17
  • 86
  • 139
howtoexpert200
  • 81
  • 1
  • 2
  • 4
  • 1
    Are you really using node.js? This code is supposed to be added in your JavaScript server's code, not in the HTML page. – Guilherme Sehn Apr 26 '15 at 05:10
  • 1
    Node.js is not in the browser. If you want to use requirejs in the browser you will need to use a script tag to load it in. – Logan Tegman Apr 26 '15 at 05:20

2 Answers2

10

require() is not built into the browser.

So when you say that "I have this code in a script tag in the head of the html page." that would explain why the symbol require is not defined when the script runs. If you want to use require() in the browser, then you need to first use a script tag to load a library that defines the require function and supports the require() type functionality and make sure that is successfully loaded before you try to use require(). requirejs is one such library that you could use.

require() is built into node.js on the server so it is always available there.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 2
    what do I need to do to have require() in angularjs? – cfl Aug 24 '15 at 11:04
  • 1
    @cfl - please re-read the middle paragraph of my answer. I tell you right there what you can use if you want `require()` like functionality in a browser. Here's an article about it: http://www.sitepoint.com/using-requirejs-angularjs-applications/ – jfriend00 Aug 24 '15 at 18:43
4

Basically you need to transform your source code for the browser, replacing require calls with the actual module code. Take a look at these utilities:

vizmi
  • 1,774
  • 17
  • 15