9

Trying to use this smartsheet api: http://smartsheet-platform.github.io/api-docs/?javascript#node.js-sample-code

and its telling me to do this for nodejs:

var client = require('smartsheet');
var smartsheet = client.createClient({accessToken:'ACCESSTOKEN'});

So i do this in my main.js file but I get the error: Uncaught ReferenceError: require is not defined

I think its because im new to nodejs/npm but I cannot find it anywhere where to actually put this require function. I think i need to mess with my node.js file but im note entirely sure. Any link to documentation or suggestions are greatly appreciated!

johnnE
  • 169
  • 1
  • 1
  • 11
  • 2
    How did u get your node application launched? – MMhunter Jul 13 '16 at 06:07
  • you know, i think that may be my first problem. i dont have a server.js thing going at all: https://github.com/TheBushyBrow/smartsheet_test – johnnE Jul 13 '16 at 06:14
  • 1
    Did you launch the file with `node main.js` through your terminal/cmd? – Roysh Jul 13 '16 at 06:14
  • i did run `node main.js` and I get `{ id: 18949130974918340, email: 'email@gmail.com', firstName: 'John', lastName: 'lastnme', locale: 'en_US', timeZone: 'US/Pacific', account: { name: 'email@gmail.com (Developer)', id: 45884085350d45924 }, admin: true, licensedSheetCreator: true, groupAdmin: true, resourceViewer: true, alternateEmails: [] }` – johnnE Jul 13 '16 at 06:19
  • So there aren't any errors when you do that... Right? – Roysh Jul 13 '16 at 06:20
  • Not in the terminal. But in the browser, the console has that `require is not defined` – johnnE Jul 13 '16 at 06:21
  • node.js is server side. It doesn't work on your browser – Roysh Jul 13 '16 at 06:22
  • Yeah, that's kinda the question. How do I get the require to work like in the example? Where do I place the require? – johnnE Jul 13 '16 at 06:23
  • 1) you can't open your main.js file in browser. 2) the only thing you can do is to write a client side app that sends a request to this file, and retrieve its data. 3) 'require' doesn't run on client side js – Roysh Jul 13 '16 at 06:25
  • Have you definitely installed the modules using `npm install`? What is the behavior you're expecting to see? – Dandy Jul 13 '16 at 06:26
  • Dandy yeah i definitely did. its in the repo. @Roysh i see.. looks like itll take a bit more time to setup the node page :/ thank you – johnnE Jul 13 '16 at 06:30
  • 1
    What you need to do: 1) Set up an Express server 2) Define in the serve the path/URL that you need to call from client side. 3) Write a frontend app and make a request to the path you've defined. Take a look at this http://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm – Roysh Jul 13 '16 at 06:33
  • @Royshsuper helpful thanks! – johnnE Jul 13 '16 at 06:34

6 Answers6

26

Try Removing "type": "module", from package.json

gyan deep
  • 1,880
  • 1
  • 13
  • 9
9

Replace all require statements to import statements. Example:

//BEFORE:
var client = require('smartsheet');
//AFTER:
import client from 'smartsheet';

Worked for me.

Noha Abuaesh
  • 535
  • 5
  • 4
2

This is because you are using node-specific calls in the browser! NodeJS is a server-side technology and not a browser technology. Thus, node-specific calls will only work in the server.

The smartsheet api you're trying to use needs to be called from the server-side code and not client side code.

For your case, you can set up ExpressJS and create an dummy api which internally calls the smartsheet api.

If you indeed want to use such calls on the client side, you can use CommonJS client side-implementations

Shubham Mittal
  • 1,545
  • 18
  • 21
0
require() does not exist in the browser/client-side JavaScript

More info on Client on Node.js: Uncaught ReferenceError: require is not defined

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Dimitry Ivanov
  • 213
  • 2
  • 4
0

you can't use require directly in browser. you need RequireJS, a JavaScript module loader.

This is introductory note from RequireJS.

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.

Jay
  • 1,575
  • 1
  • 17
  • 22
0

Technically it is inbuilt in the browser and the best thing to do is disable the es-lint in the project and all these errors will not occur. Or try out commenting the whole es-lint file.

It worked for me!

Amit Kumar
  • 51
  • 1
  • 4