ReferenceError: require is not defined (Paynow) - javascript

I understand a similar question has been asked before. I am trying to import a payment gateway module (paynow/nodeJS) into a web application. Browser console is displaying
"ReferenceError: require is not defined"
at line
const { Paynow } = require("paynow");
What is that I could be doing wrong?

There are two likely causes of this:
You are trying to run the JS in a browser instead of in Node.js
You have Node.js configured to use ES6 modules instead of CommonJS modules
If the former, run the code designed to be run in Node.js in Node.js.
If the latter, either use import instead or configure the system to use CommonJS modules (don't use the .mjs file extension and don't use package.json's type field.

Related

Preserving node.js built-in imports for Electron in rollup

I am making a Electron app with Svelte and Typescript.
I started with this template for that exact purpose, but it disables node.js built-in imports (like fs) in the browser/electron frontend for security.
I do not need this improved security in my project, so I am trying to get node.js fs to work in the Electron browser.
I already modified the Electron Backend script that creates the Browser to re-enable nodeIntegration, and this works: using require("fs") in the Electron browser console logs the fs library.
Using this in the actual typescript frontend code does not work, however. From looking at the bundled JS, it seems like rollup is assuming that the import of fs is just available as a global variable, and trying to guess its name.
When building while importing fs and path, I get the following warnings:
(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on "path". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node
(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
fs (guessing 'fs')
path (guessing 'path')
The first warning suggests a 404 GitHub link that seems to be a polyfill for some Node built-in libraries. This isn't what I want, I want the real node.js fs library. It also informs me that I'm creating a browser bundle - I have tried setting the browser option of #rollup/plugin-node-resolve (used by the template) to false, but this did not help.
The second warning seems to simply inform that it's trying to guess global variable names - which it should not, it should keep the imports.
How do I allow importing Node.js modules here? The linked template project still closely resembles my current one.
Help is greatly appreciated.
Turns out that the deciding factor was the output.format of the rollup.config.js.
This was set to iife, which produced a result without require or import.
Changing it to cjs solves this problem.

Test javascript in node's public folder using jest

I have a simple javascript function in an example.js file placed inside the node js public directory.
I am using jest for unit testing.
The problem is if I write the following in the example.js javascript file in the public folder:
module.exports.myFunction = myFunction;
Jest test file is able to import it, using require() and perform tests, however when I run the web application, the browser complains when I service the page containing this javascript:
Uncaught ReferenceError: module is not defined
What is the correct way to test javascript files in the public directory of the node application?
Using export and/or import in the project is reported an being unrecognized and results in errors as well.
How is this done?
As mentioned in comments require(...) and module.exports relate to Common JS Modules, which are natively supported by NodeJS runtime, but not by browser. So basically you'll need to add extra build configuration to have your module work in both runtimes.
If you want to have outputs in both CommonJs and Browser friendly bundle - you can write all code in ES Modules and use build tools like webpack to provide outputs in different formats.
Also, starting from Node 13.2.0 - it supports ES modules natively. So I would stick to ES modules for ongoing development anyway.
Please also check this short article on main JS module format differences.

Uncaught ReferenceError: require is not defined?

I have downloaded node-js and I am using npm inside a folder. I have downloaded the lodash package as a dependency and it is in the node modules folder.
but when I try to use it using require() it is giving above mentioned error in the console and suggesting to use ES6 module by vs code and require is a module of common.js
I have a package.json file and lodash is listed as a dependency. I am able to run lodash in the command line using node server but not in the browser
Do we need any other package installed in order for require() function to run other than node-js?
what am I doing wrong?
I am on windows and using the latest version of node and npm
let a = require('lodash');
If VSCode is suggesting you use ES6 modules, then you may have turned on ES6 Module support in your project which overrides Node's default support for CommonJS modules.
Use
import lodash from 'lodash';
instead of require.
Re edit:
I am able to run lodash in the command line using node server but not in the browser
Well yes. If you run a program designed to work in Node.js in Node.js then it work. If you run the same program in a browser, then it won't work. Just having Node.js installed somewhere doesn't turn the browser into Node.js.
If you want to use an ES6 module in a browser then:
It must be compatible with browsers (lodash might be)
You need to use import lodash from "./url/to/lodash.js"; because browsers don't have support for resolving npm paths.
If the module isn't designed to run in browsers, then you might be able to use a tool like Webpack to bundle it up in a way that will work (but that won't work in the module depends on APIs provided by Node.js, like fs).
You can use express framework to work on NODE js
let express = require('express') ;

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!
Try Removing "type": "module", from package.json
Replace all require statements to import statements. Example:
//BEFORE:
var client = require('smartsheet');
//AFTER:
import client from 'smartsheet';
Worked for me.
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
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.
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!
require() does not exist in the browser/client-side JavaScript
More info on Client on Node.js: Uncaught ReferenceError: require is not defined

'Require is not defined' in Netbeans - Javascript

I am new to Javascript and am interested in using a library from github. I am using netbeans to code and I have installed node.js. However, I am still getting the error 'Require is not defined'. I have installed 'browserify' as this seemed like a common solution, but I am still getting this error.
Am I doing something wrong?
Image of set up libraries
Update
I have also found that there is a problem with one of my libraries, think it could be relevant to the original problem.
Problem with library
If you are developing NodeJS based project, you should use NodeJS project type in NetBeans where require() is considered as known global function and as such NetBeans won't show the hint.You can change your current project to enable NodeJS support by right clicking on the project, select Project Properties -> NodeJS and check Enable NodeJS support.
If you are using RequireJS library, you can also enable RequireJS support in Project Properties in JavaScript Frameworks -> RequireJS
I guess this is because require() does not exist in the browser/client-side JavaScript.Can you give it a try to following statements;
Use <script> tag.
Use a CommmonJS implementation. Synchronous
dependencies like Node.js
Use an AMD implementation.
And keep library codes and application codes seperated. ( bundle.js and script.js )
Browserify will take all the script files necessary and put them into the "bundle.js" file, so you should only have to include "bundle.js" in the HTML file, not the "script.js" file.

Categories