-1
const nodemailer = require('nodemailer');
const { google } = require('googleapis');

I wrote the code for lessons on the Internet for the node js. But I need to do this for the site so I copied it. After running it gave me this problem (not with command node app.js, it starts in browser as default js code): Uncaught ReferenceError: require is not defined. Please tell me what can be done.

Oleg
  • 41
  • 4
  • 1
    Does this answer your question? [Client on Node.js: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-js-uncaught-referenceerror-require-is-not-defined) – grekier Aug 02 '21 at 22:23
  • 1
    Check out this question/answer: https://stackoverflow.com/a/19059825/1540177 – grekier Aug 02 '21 at 22:24

1 Answers1

0

In web browser we don't have the require function.

You can use the import statement or use cdn services like that:

<script src="https://apis.google.com/js/api.js"></script>

importing directly like:

import { google } from 'googleapis';

won't work on web browsers because the browser does not know where 'googleapis' is.

Neriya Cohen
  • 110
  • 8
  • Cannot use import statement outside a module. Why? – Oleg Aug 03 '21 at 07:19
  • You need the script to be a module, this can be done with the `type='module'` attribute. Read [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) from MDN – Neriya Cohen Aug 03 '21 at 07:53
  • `Uncaught SyntaxError: The requested module 'https://apis.google.com/js/api.js' does not provide an export named 'google'` – Oleg Aug 03 '21 at 08:07