hashids: ReferenceError: require is not defined - javascript

The following gives me an issue saying ReferenceError: require is not defined
var Hashids = require('hashids'), hashids = new Hashids('my salt', 8);
I am using this hashids.js class.
The description says that this is a client-side version of Node.js version. I am not using bower so I just load the hashids.js on the page.
Are there any other javascript files I need to have this hashids.js is dependent on?
How do I get it working?

You can't use node code in client-side unless you use library like browserify to load the module.
Try this instead -> https://github.com/ivanakimov/hashids.js/blob/master/lib/hashids.js

Related

Require module inside a WebWorker in React

i am trying to import an npm module inside my React Web Worker using const package = require('package-name').
But it gives me the following error :
Uncaught (in promise) ReferenceError: require is not defined
How can i use a module inside a worker, is there a native way or some way to bypass it?
I am using NodeJS v18.10.0, Webpack v5.73.0.
I think your question is related to
Client on Node.js: Uncaught ReferenceError: require is not defined
please look at it , you might solved your problem

Problems using aws-sdk in the browser with browserify

I am working on browser based application that makes use of aws-sdk. I am using browserify for my app code but have not figured out how to roll aws into it. I have tried a couple of different approaches:
//MyApp.js - Take 1 using downloaded minified version
var AWS = require ('./aws-sdk.min.js');
...
AWS.config.region='us-east-2';
...
results in Cannot set property 'region' of undefined
My guess is that this doesn't work because browserify does not resolve the minified code.
//MyApp.js - Take 2 using downloaded development version
var AWS = require ('./aws-sdk.js');
This does not compile. Browserify reports Error: Cannot find module '../lib/core'.
Is there a trick to this that I am missing?
When I used AWS in the browser I set my region depending on the service I need, for example:
new AWS.EC2({apiVersion: '2016-11-15', credentials, region})
So this made me wonder, maybe the version you downloaded is encapsulated and not exposing anything for browserify.
First I tested the version in the browser as follows:
console.log(AWS)
<script src="https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.184.0/aws-sdk.min.js"></script>
Everything looked good so then I went ahead and tested on browserify.
Turns out you are reassigning the AWS global variable when you do:
var AWS = require ('./aws-sdk.min.js');
But you are already bundling it, so you are good, what you need to do is the following:
require ('./aws-sdk.min.js');
// And then use it happily
AWS.config.region='us-east-2';
Without reassigning the AWS global variable

Require is not working for node-opcua

I want to load a local version of node-opcua with 'require' inside a HTML file, but it does not really work. The code snippet is the following:
<script type="text/javascript" src="path_to_require.js"></script>
<script>
var opcua = require(["path_to_node-opcua"]); <!-- Yes, the path is correct >
var client = new opcua.OPCUAClient();
...
When I execute the script I get the following error in the console:
Uncaught TypeError: opcua.OPCUAClient is not a constructor
Hence, var opcua is loaded correctly, but OPCUACluent is not, although the class is declared in a file that is present in the node-opcua folder called opcua_client.js under node-opcua\lib\client\
Sources:
The 'require' script from http://requirejs.org/docs/download.html#requirejs.
The node-opcua folder with the console command
npm install node-opcua.
node-opcua is not intended to run inside a browser as it relies on nodejs specific features such as filesystem access, crypto and so on.
You need to use browserify if you want to use that module in client. You will also need to look at how to use browserify with file system access (it can be done if paths are known ahead of time).

Have Multiple js Files in a Firefox addon that can have require method?

I am building an add-on that has multiple .js files that are associated with it many of which need the access to the require() function but when i use the require function in them i get the error that require is not defined but in the index file this works properly.
ie. I have a page in which i need access to the Browser's Local storage but when i try to use the simple-storage API it generates error.
CODE : var storage_object = require("sdk/simple-storage");
ERROR : JPM undefined Message: ReferenceError: require is not defined
Is there a way by which i can have multiple files that have the require function and if not then how can I include the API's that are required in those .js files for proper functioning?
Worker files can all use the require method :) Just have to importScripts('resource://gre/modules/workers/require.js'); can read here: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
Im not sure where the require.js documentation is on MDN.

What is this error that comes up in node.js?

I was trying to follow this tutorial.
Then when I got to this part
node jsctags/bin/jsctags --sort=yes --locals tst.js
I got the following warning message.
The "sys" module is now called "util". It should have a similar
interface.
I'm doing this in OSX. First I tried the package and then I tried installing from source. I still get the same message.
What does this mean? What can I do about it?
In your jsctags file you probably have a line that looks like this:
sys = require ('sys');
As a first step, try using this line:
sys = require ('util');
This will still refer to the package by the name sys in your script, so the rest should work; but in the future, util = require ('util'); might be better, to make it more clear that you're using the newer package and API.
The message is just Node's way of telling your sys module is deprecated and everyone should migrate to util.
The reason you're getting it may not be your fault. If any of the libraries you're using not migrated to the new modules, it'll also show that message.

Categories

Resources