Fred J. Fred J. - 1 year ago 64
Javascript Question

Is Import needed to run Meteor app

This Meteor template code is the default generated when

meteor create myApp
is invoked. In
the first 3 lines are:

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';

But when I comment them out, the app still runs. Reading the docs could not answer my question, Why do we need the
statement if the app still runs without them? Thanks

Answer Source

Earlier versions of Meteor relied heavily on using the global namespace for accessing shared libraries. As of Meteor 1.3, you can now use Meteor's ES2015 module support. This means you can use import/export functionality to expose access to various parts of your codebase, without having to rely on globals. Using imports/exports is now the preferred/recommended way of referencing parts of your application, so meteor create functionality was updated to demonstrate this. As you mentioned, you can remove the import statements above and you will still be able to access Template and ReactiveVar globally (for backwards compatibility). It's important to note however that this might change in the future - Meteor has fully embraced ES2015 module support, and could potentially drop the use of globals completely (well, to the extent possible).

One more thing to note - Meteor 1.3 also introduced new "lazy-loading" functionality, wherein application code stored under an /imports directory will no longer by eagerly loaded when the application starts up. Code stored under an /imports directory will only be loaded if it's referenced by an import statement somewhere else in your codebase. See Special directories for more info.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download