Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require is not defined nodejs

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!

like image 647
johnnE Avatar asked Jul 13 '16 06:07

johnnE


People also ask

How do you fix require is not defined in NodeJS?

To fix this, remove "type": "module" from your package. json and make sure you don't have any files ending with . mjs . This can happen with anything in your project folder.

How do you define require in NodeJS?

You can think of the require module as the command and the module module as the organizer of all required modules. Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file'); The main object exported by the require module is a function (as used in the above example).

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.

Why is require not defined?

Your ReferenceError: require is not defined likely has one of two causes: You tried using require in a browser environment. You are in a Node. js environment but your project has "type": "module" in its package.


1 Answers

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

like image 168
gyan deep Avatar answered Oct 15 '22 02:10

gyan deep