Require Is Not Defined? Node.Js

require is not defined? Node.js

In the terminal, you are running the node application and it is running your script. That is a very different execution environment than directly running your script in the browser. While the Javascript language is largely the same (both V8 if you're running the Chrome browser), the rest of the execution environment such as libraries available are not the same.

node.js is a server-side Javascript execution environment that combines the V8 Javascript engine with a bunch of server-side libraries. require() is one such feature that node.js adds to the environment. So, when you run node in the terminal, you are running an environment that contains require().

require() is not a feature that is built into the browser. That is a specific feature of node.js, not of a browser. So, when you try to have the browser run your script, it does not have require().

There are ways to run some forms of node.js code in a browser (but not all). For example, you can get browser substitutes for require() that work similarly (though not identically).

But, you won't be running a web server in your browser as that is not something the browser has the capability to do.


You may be interested in browserify which lets you use node-style modules in a browser using require() statements.

Uncaught ReferenceError: require is not defined in nodeJs

You need to run your Node.js code using Node.js.

Linking to it with a <script> element and trying to run it using a web browser won't work. Web browsers are not Node.js.

Web browsers do not have features to support running servers. Web browsers do not support CommonJS modules. Web browsers do not support Node.js style module resolution where they search a node_modules directory when you import from a name instead of a URL.

You need to use Node.js.

Require is not defined nodejs

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

Require is not defined when running in node.js

Remove "type": "module" from your package.json. This tells Node you're using ESM rather than CJS, but that's not what you want to be doing (unless you do, in which case you should use the .mjs extension and use import instead of require.



Related Topics



Leave a reply



Submit