Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add third-party JavaScript libraries to a Meteor application?

I want to add a JavaScript front-end plugin, like jquery.center.js, to a Meteor app.

If I put it in my app/ directory and refresh the page I get this error:

Your app is crashing. Here's the latest log.

node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: jQuery is not defined
at app/jquery.center.js:43:1
at /Users/crapthings/Desktop/app/.meteor/local/build/server/server.js:111:21
at Array.forEach (native)
at Function. (/Users/crapthings/Desktop/app/.meteor/local/build/server/underscore.js:76:11)
at /Users/crapthings/Desktop/app/.meteor/local/build/server/server.js:97:7
Exited with code: 1
Your application is crashing. Waiting for file change.

like image 382
crapthings Avatar asked Jun 13 '12 07:06

crapthings


People also ask

How can we add a library in JavaScript?

Use the Add . js file to browse and select a . js file to add to your JavaScript user library. Use the Add folder button to browse and select a folder to add to your JavaScript user library.

Where should I put JavaScript libraries?

js file, you need to put it in the same place as the rest of your project. For simplicity's sake, put it in the same folder that your sketch. js is in. If you're using the p5js web editor, use the dropdown menu in the side bar to select Upload file and then upload the library file from where you saved it earlier.

How can we add an external js library to a webpage?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

What are third-party libraries JavaScript?

Third-party JavaScript applications are self-contained components, typically small scripts or widgets, that add functionality to websites. As the name implies, they're offered by independent organizations, with code and asset files served from a remote web address.


4 Answers

You are putting jquery plugin javascript file in app folder directly,so that javascript file will be be loaded for client as well as server.

As per Meteor documentation:
Client loads javascript from: project/public and project/client
Server loads javascript from: project/public and project/server folders.

As of v1.0, Meteor is using jQuery internally in the client, so you can use your library directly without adding jQuery. However, it's recommended that you add jQuery to your Meteor project explicitly:

meteor add jquery

The Meteor docs explain in depth how JavaScript files are loaded and where static assets should go (CSS, images).

See also how to repackage an existing library for Meteor.

like image 195
Nachiket Avatar answered Sep 22 '22 21:09

Nachiket


Put it inside the client folder such that it is only loaded on the client, no need for jQuery on server.

like image 40
Tamara Wijsman Avatar answered Sep 25 '22 21:09

Tamara Wijsman


One way to do this in MeterorJS 1.3.x

Add the JS files in the public\js\ directory

enter image description here

Load it up from Meteor.startup method using $.getScript in client/main.js If you want to control script load sequence, control with multiple $.getScript for each js files.

enter image description here

Meteor.startup(function(){
    $.getScript('js/fhir-client.js', function(){
        // script should be loaded and do something with it. 

    });
});
like image 22
Gajen Sunthara Avatar answered Sep 21 '22 21:09

Gajen Sunthara


Starting with Meteor 1.3, you can add 3rd party JavaScript libraries to a Meteor project directly via their npm package:

meteor npm install --save moment

Both server-side and client-side package work without modification because Meteor's ES2015 module system takes care of creating a Node-like environment in the client much as browserify or webpack do.

If an npm package happens to not function correctly, look for a wrapper on Atmoshpere. Atmosphere is Meteor's official 3rd package repository, but less and less relevant after Meteor v1.3. It will eventuall be phased out.

History

Before Meteor 1.3, you had to repackage 3rd party libraries for Meteor. A tool called Autopublish was developed to automate the process. After the Meteor Development Group stopped offering free hosting at meteor.com, Autopublish was discontinued.

like image 23
Dan Dascalescu Avatar answered Sep 25 '22 21:09

Dan Dascalescu