My HTML file:
<!DOCTYPE html>
<head>
...
</head>
<body>
...
<script src="script.js"></script>
</body>
My JavaScript file - script.js:
import * as FilePond from 'filepond';
const myPond = FilePond.create({
multiple: false,
name: 'filepond'
});
document.body.appendChild(pond.element);
But error occurred, the browser said:
Uncaught SyntaxError: Cannot use import statement outside a module
So I edited the script.js into this:
const FilePon = require('filepond')
const myPond = FilePond.create({
multiple: false,
name: 'filepond'
});
document.body.appendChild(pond.element);
But error occurred again, the browser said:
Uncaught ReferenceError: require is not defined at script.js:1
How can I fix it?
It's simple:
Just include the FilePond css & js file from CDN instead like:
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
Next, you do not need any import or require. You can simply use the rest of the code as FilePond is globaly declared now like:
const myPond = FilePond.create({
multiple: false,
name: 'filepond'
});
document.body.appendChild(myPond.element);
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
require() is a NodeJS function, not a browser JS function.
If the package uses npm, chances are its made for NodeJS and not browser JS.
If you want to include js files in the browser, you need to use html includes:
<script src="script.js"></script>
Or a templating solution which allows to include other files such as EJS
Actually, require() is for Node.js. You can't use it in browsers.
First solution:
Add the type="module" attribute to the <script> tag.
So it will be <script type="module" src="script.js"></script>
Second solution:
Just add <script src="https://cdn.jsdelivr.net/npm/filepond#4.13.6/dist/filepond.js"></script> and <link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet"> before calling script.js
I think this will work for you:
<script src="https://cdn.jsdelivr.net/npm/filepond#4.13.6/dist/filepond.js"></script>
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
<script>
const myPond = FilePond.create({
multiple: false,
name: 'filepond'
});
document.body.appendChild(pond.element);
</script>
Related
I am using Ionic with vanila java script
I am using CDN in head component
<script type="module" src="https://cdn.jsdelivr.net/npm/#ionic/core/dist/ionic/ionic.esm.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/#ionic/core/dist/ionic/ionic.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/#ionic/core/css/ionic.bundle.css"/>
In the body of HTML
<ion-alert-controller></ion-alert-controller>
......
.....
<script src="app.js"></script>
in app.js
const alertCtrl = document.querySelector('ion-alert-controller');
..
..
..
alertCtrl.create ({
message: 'ABC',
header: 'DEF',
button: ['Okay']
});
getting an error saying
app.js:29 Uncaught TypeError: alertCtrl.create is not a function
at HTMLElement. (app.js:29)
I'm not used to code with vanilla, but according to Ionic docs for js, you should use alertController this way :
const alertCtrl = document.querySelector('ion-alert-controller');
alertCtrl.message ='ABC';
alertCtrl.header='DEF';
alertCtrl.button= ['OK'];
I faced the same situation.
The ionic CDN you are using will download the latest version of ionic, however I guess ion-alert-controller directive is ionic 4.
you need to use
<link href="https://unpkg.com/#ionic/core#4.0.2/css/ionic.bundle.css" rel="stylesheet">
<script src="https://unpkg.com/#ionic/core#4.0.2/dist/ionic.js"></script>
Note that the links above refer to unpkg.com, not jsdelivr.net
Please refer to
https://unpkg.com/browse/#ionic/core#4.1.0-dev.201902272232.d66b12b/README.md
lines 26 & 27
I have paperscript code in offset-utils.js file. It contain:
var OffsetUtils = new function() { ... }
I load it in index.html:
<script src="vendor/vendor.js"></script>
<script src="utils.js"></script>
<script src="offset-utils.js" type="text/paperscript" canvas="canvas" ></script> <!-- -->
<script src="index.js"></script>
paper.js is included in vendor.js
In my code in index.js I got error
Uncaught ReferenceError: OffsetUtils is not defined
How I can load my paperscript library for use it in javascript code?
If you're declaring it with var then it's local to that source file. You need to export it, like:
window.OffsetUtils = new function() ...
I am trying to build a demo app with Vue.js. What I am getting is an odd error that Vue is not defined.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue JS Intro</title>
</head>
<body>
<div id="demo">
<p>{{message}}</p>
<input v-model="message">
</div>
<script type="JavaScript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
<script>
var demo = new Vue({
el: '#demo',
data: {
message: 'Hello Vue.js!'
}
})
</script>
</body>
</html>
What did I miss here? This is not a CDN issue as I also downloaded the library from the official website, used it and got the same result
index.html:15 Uncaught ReferenceError: Vue is not defined
jsBin demo
You missed the order, first goes:
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
and then:
<script>
var demo = new Vue({
el: '#demo',
data: {
message: 'Hello Vue.js!'
}
});
</script>
And type="JavaScript" should be type="text/javascript" (or rather nothing at all)
Sometimes the problem may be if you import that like this:
const Vue = window.vue;
this may overwrite the original Vue reference.
try to fix type="JavaScript" to type="text/javascript" in you vue.js srcipt tag, or just remove it.
Modern browsers will take script tag as javascript as default.
I needed to add the script below to index.html inside the HEAD tag.
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
But in your case, since you don't have index.html, just add it to your HEAD tag instead.
So it's like:
<!doctype html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
...
</body>
</html>
I got this error but as undefined due to the way I included js files
Initailly i had
<script src="~/vue/blade-account-update-credit-card-vue.js" asp-append-version="true"></script>
<script src="~/lib/vue/vue_v2.5.16.js"></script>
in the end of my .cshtml page
GOT Error Vue not Defined
but later I changed it to
<script src="~/lib/vue/vue_v2.5.16.js"></script>
<script src="~/vue/blade-account-update-credit-card-vue.js" asp-append-version="true"></script>
and magically it worked. So i assume that vue js needs to be loaded on top of the .vue
I found two main problems with that implementation. First, when you import the vue.js script you use type="JavaScript" as content-type which is wrong. You should remove this type parameter because by default script tags have text/javascript as default content-type. Or, just replace the type parameter with the correct content-type which is type="text/javascript".
The second problem is that your script is embedded in the same HTML file means that it may be triggered first and probably the vue.js file was not loaded yet. You can fix this using a jQuery snippet $(function(){ /* ... */ }); or adding a javascript function as shown in this example:
// Verifies if the document is ready
function ready(f) {
/in/.test(document.readyState) ? setTimeout('ready(' + f + ')', 9) : f();
}
ready(function() {
var demo = new Vue({
el: '#demo',
data: {
message: 'Hello Vue.js!'
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<p>{{message}}</p>
<input v-model="message">
</div>
as extra information,
if you use VueJs version ^3.2.0 and up, the way you should write Vueis is different witch you have the app.js has this code:
import "./bootstrap";
import { createApp } from "vue";
const app = createApp({});
So you have to use app object instead of Vue as you see bellow
import VueCountdownTimer from "vuejs-countdown-timer";
app.use(VueCountdownTimer);
I shared this information because this error counted me.
just put text/javascript in your script tag which you use for cdn
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js" type="text/javascript"></script>
I had the same problem and for a strange reason, the js that contains the Vue, has a defer inside the script definition:
<script src="js/app.js" defer></script>
That caused the script loads asynchronously than the other js files.
After I remove the defer tag, the problem was solved.
For those who are facing this error in src/main.js file in vue,
just add :-
var Vue = require('vue')
Then, It will work fine.
I am trying to update an old cometd javascript wrapper and test client (was 1.3.x) that I have to the newer comet 2.5.1 javascript implementation. I have all of the dependencies and the browser can find them all, yet I am getting errors in Firebug's console (see below)
The head of my HTML is as below:
<head>
<title>CometD Tester</title>
<link rel="stylesheet" type="text/css"href="style/style.css" />
<script type="text/javascript" src="org/cometd/Cometd.js"></script>
<script type="text/javascript" src="org/cometd/AckExtension.js"></script>
<script type="text/javascript" src="org/cometd/ReloadExtension.js"></script>
<script type="text/javascript" src="jquery/jquery-1.9.0.js"></script>
<script type="text/javascript" src="jquery/jquery.cookie.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd-reload.js"></script>
<script type="text/javascript" src="js/myCometd.js"></script>
</head>
All of these are found by the browser. Looking at Cometd.js I see the following:
org.cometd.Cometd = function(name)
{
....
}
So is that not defining org? Note that none of the errors in the Console are from Cometd.js. Otherwise I see no other definition of "org.cometd". I would really appreciate it if anyone can help me out. I am using Tomcat 7 and below is the dir structure:
Thanks.
UPDATE - Further testing
I reduced the header to:
<head>
<title>CometD Tester</title>
<link rel="stylesheet" type="text/css"href="style/style.css" />
<script type="text/javascript" src="org/cometd/Cometd.js"></script>
</head>
And removed ALL JS from the index.html. The only JS now included is the Cometd.js from the comet.org. There is still the same error... coming from the very first line in that script:
org.cometd.Cometd = function(name)
Not sure what I have missed here.
EDIT - Add jquery.cometd-reload.js
This is the contents of the file. It looks like it is "re-binding" functionality from the cometd library to use the jquery one instead (?). I'm not up to speed enough in JS to debug this (I'm a C++ dev really).
(function($)
{
function bind(org_cometd, cookie, ReloadExtension, cometd)
{
// Remap cometd COOKIE functions to jquery cookie functions
// Avoid to set to undefined if the jquery cookie plugin is not present
if (cookie)
{
org_cometd.COOKIE.set = cookie;
org_cometd.COOKIE.get = cookie;
}
var result = new ReloadExtension();
cometd.registerExtension('reload', result);
return result;
}
if (typeof define === 'function' && define.amd)
{
define(['org/cometd', 'jquery.cookie', 'org/cometd/ReloadExtension', 'jquery.cometd'], bind);
}
else
{
bind(org.cometd, $.cookie, org.cometd.ReloadExtension, $.cometd);
}
})(jQuery);
So the problem was that I misunderstood the project layout from the Comet.org site. I should have followed the direction posted at cometd primer for non-maven setups a lot more closely. Basically when you are setting up the project you download the distribution, and then you need to take the code from the war files bundled inside the tarball.
SO, once you have extracted the tarball...
Take the org folder from cometd-javascript-common-2.5.1.war (located in \cometd-2.5.1\cometd-javascript\jquery\target) or cometd-javascript-jquery-2.5.1.war (located in \cometd-2.5.1\cometd-javascript\common\target)
Take the jquery folder from cometd-javascript-jquery-2.5.1.war
The org namespace definition was in the file org/cometd.js which I did not have before, as I wrongly assumed that it had been replace by the org/cometd/Cometd.js file. The namespaces org and comet are defined as below starting on line 17 of that file:
// Namespaces for the cometd implementation
this.org = this.org || {};
org.cometd = {};
org.cometd.JSON = {};
The functions are working correctly now.
Try loading jQuery before any of the other JavaScript files -
<head>
<title>CometD Tester</title>
<link rel="stylesheet" type="text/css"href="style/style.css" />
<script type="text/javascript" src="jquery/jquery-1.9.0.js"></script> <!-- load first -->
<script type="text/javascript" src="org/cometd/Cometd.js"></script>
<script type="text/javascript" src="org/cometd/AckExtension.js"></script>
<script type="text/javascript" src="org/cometd/ReloadExtension.js"></script>
<script type="text/javascript" src="jquery/jquery.cookie.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd.js"></script>
<script type="text/javascript" src="jquery/jquery.cometd-reload.js"></script>
<script type="text/javascript" src="js/myCometd.js"></script>
</head>
I am trying to include CodeMirror Plugin but am getting this error
Uncaught ReferenceError: CodeMirror is not defined
My HTML CODE is here
http://pastie.org/4673008
Can anyone help ??
Thanks in advance..
You're loading the clike module before CodeMirror itself.
Change the order in your HTML head from this:
<script type="text/javascript" src="../CodeMirror-2.33/mode/clike/clike.js"></script>
<script type="text/javascript" src="../CodeMirror-2.33/lib/codemirror.js"></script>
<script type="text/javascript" src="../CodeMirror-2.33/mode/javascript/javascript.js"></script>
To this:
<script type="text/javascript" src="../CodeMirror-2.33/lib/codemirror.js"></script>
<script type="text/javascript" src="../CodeMirror-2.33/mode/clike/clike.js"></script>
<script type="text/javascript" src="../CodeMirror-2.33/mode/javascript/javascript.js"></script>
ngx-codemirror SQL high-late is not working, deeper I found the to the code mirror tag the class to high-late syntax cm-default, cm-link, cm-keyword these class is not appending.
config used :
public queryConfig = {
lineNumbers: true,
theme: 'twilight',
mode: 'text/x-sql',
lineWrapping : true,
autoFocus: true
};
The main.ts file added the imports below:
The angularcli.json file added the scripts below: