Client-side javascript error: Uncaught ReferenceError: require is not defined - javascript

I'm trying to create a new Range object in Ace Editor. I've found numerous examples prescribing the following:
var Range = require("ace/range").Range;
var newRange = new Range(0, 0, 0, 10);
But when I try this I get the following error:
Uncaught ReferenceError: require is not defined
I'm loading the Ace Editor JS in a script tag in a Rails view:
<script src="/js/ace_editor/ace.js" type="text/javascript" charset="utf-8"></script>

If you are using no-conflict version you need to use ace.require instead of require, since no-conflict doesn't create global require to not conflict with other incompatible implementations of require, that might be loaded on the page.

Related

Bouncer.js in NextJS React is Undefined

Good Day.
Banging my head here and need a pair of fresh eyes...
I have a NextJS (10.0.6) app (React 17.0.1). I'm trying to incorporate Bouncer.js validation code.
Both the bouncer.min.js file and my init() file are being pulled into the correct page,
<script src="/static/bouncer.min.js" async=""></script>
<script src="/static/bouncerLoad.js" async=""></script>
</body>
which I believe is evident because I'm getting an Undefined error.
Unhandled Runtime Error
ReferenceError: Bouncer is not defined
This is the content of my bouncerLoad.js file:
var validate = new Bouncer('contactform');
console.log("bouncer loaded");
Any suggestions?

Meteor jsPDF Package Error

I added js-pdf package to my Meteor app but whenever I run a simple hello world code I get an error:
Code:
var doc = new jsPDF();
doc.setFontSize(40);
doc.text(35, 25, "Paranyan loves jsPDF");
I get:
[Error] ReferenceError: Can't find variable: jsPDF
click #download-pdf (manageatas.js, line 53)
(anonymous function) (templating.js, line 120)
(anonymous function) (blaze.js, line 2205)
withCurrentView (blaze.js, line 2038)
(anonymous function) (blaze.js, line 2204)
(anonymous function) (blaze.js, line 802)
dispatch (jquery.js, line 4657)
handle (jquery.js, line 4325)
I tried with the package installed, I tried downloading the code and adding it to client side folder but I get the same error again ...
What's wrong ?
I've had a load of problems with that package too.
Try putting jspdf.debug.js
somewhere in your code (I have mine in client/thirdparty/ ), get rid of the var from var jsPDF = (function(global) and give it a shot this way.
Make sure to remove the package and other jspdf js files first!
The package appears to be incompatible with recent Meteor versions (which is weird, as it seems to receive updates). I recommend adding jspdf.js manually to your /client/lib/compatibility folder.
If you don't want to use compatibility folder, you need to ensure the jsPDF object is created in global scope, instead as a local variable in a file. Fortunately, it's easy – just remove var keyword from its declaration. In other words, line 39 should be:
jsPDF = (function () {
instead of
var jsPDF = (function () {
use html-pdf npm package to create dynamic html to pdf file. its very easy to use
Steps:
create html file for report layout design
pass dynamic data in json format by SRS templating methods and get html code.
pass html code in following function
Code here:
var fs = require('fs');
var pdf = require('html-pdf');
var options = { format: 'Letter' };
pdf.create(html, options).toFile('./businesscard.pdf`enter code here`', function(err, res) {
if (err) return console.log(err);
console.log(res); // { filename: '/app/businesscard.pdf' }
});
where html stand for HTML Code.
visit https://www.npmjs.com/package/html-pdf

elasticsearch.js: ReferenceError: require is not defined

I'm trying to create a project using elasticsearch.js. I'm following the docs found here.
In the <head> I've loaded:
<script src="js/jquery-1.10.2.js"></script>
<script src="js/elasticsearch.js"></script>
<script src="js/esInit.js"></script>
and in custom.js I have
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
{Host Here}
});
In FB console I see
ReferenceError: require is not defined
var elasticsearch = require('elasticsearch');
I'm reading that require() is not a function of javascript. Very confused.
It looks like you are loading the version that is supposed to be used with Node.js.
There are browser compatible builds available as well: http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/browser-builds.html
Yes require is not a default function in javascript. Instead you need to download that js file from "http://requirejs.org/docs/download.html"

Using jQuery plugins with RequireJS

I'm using the latest version of RequireJS and trying to get it to work with jQuery and jQuery plugins.
I can get it to load jQuery fine, but I can't get it to work with jQuery plugins. This is my code:
var dependencies = ["order!jquery"];
dependencies = dependencies.concat([
"order!plugins/underscore-1.3.1",
"order!plugins/backbone-0.9.1",
"order!plugins/jquery.typewatch"]);
require.config({
waitSeconds: 30,
paths: {
"jquery": 'http://code.jquery.com/jquery-1.9.1'
}
});
require(dependencies, function($) {
...
jQuery is loading OK, but I then get
Uncaught ReferenceError: jQuery is not defined jquery.typewatch.js:92
Uncaught TypeError: undefined is not a function backbone-0.9.1.js:1064
The first error is complaining about jQuery not being defined, and the second about $ not being defined.
Why isn't this working? Everything seems to be loading in the right order, the order plugin works OK for underscore and backbone, and I can use $ in the code for normal jQuery functions.

JS Lint for Visual Studio 2010 problems

I have a file with a JS object:
function Monitor() {
var self = this;
...
And I have a file that creates an instance of this and uses it.
self.monitor = new Monitor();
The files are included in a cshtml file in order:
<script type="text/javascript" src="#Url.Content("~/Scripts/Shared/Monitor.js")"> </script>
<script type="text/javascript" src="#Url.Content("~/Scripts/Views/NewMonitor_Index.js")"></script>
The problem is I get this error:
Warning 1 JS Hint: 'Monitor' is not defined.
How do I configure it so that it finds the monitor object?
I don't think if there is an automatic way. Although JSHint could detect other script tags, it is probably more difficult to get the actual path to the file.
Anyways, if I know that a certain symbol is definitely available in the context, I add a
/*global Monitor*/
at the beginning of the script.
If a symbol will be available in every script, I add it to my .jshintrc file in the directory, like
{
"predef": [
"Monitor"
]
}
But I don't know if/how this works on Windows.

Categories