Is `import { Session }` needed?

I am creating a React-Meteor app, and have created a Session variable to make certain data available globally. To do this, from the root folder of the app, I ran meteor add session.

The docs show for each method an import statement:

Session.methodName(parameters)
import { Session } from 'meteor/session'

In my project, I have commented out every single import { Session } statement, and it still works. Is this because I am only using .get() and .set(), or is the import statement in fact not needed?

This is for backwards compatibility. In early times, Meteor worked with magic globals that were available everywhere. They still work, but you should better import them to make things clearer.

1 Like

To add on to this, I’m reasonably sure this is just true for atmosphere packages?

I wasn’t around during the blaze era, but I’ve definitely noticed I didn’t need to import things when using atmosphere packages.

Definitely agree you should explicitely import them, though. It’s much easier to read.

IIRC, this worked everywhere. You could even define your own “global” variables by assigning them without putting var, let or const in front of them, like this:

myGlobal = true;

Once you put an explicit variable declarator in front, however,

var myVariable = true;

the scope was limited to the file the variable was defined in.

In packages, you additionally have to export the package-wide globals via a statement in package.js, to make them global for the entire application.

Read somewhere the Meteor “might” remove the global nature of these meteor packages. So import them as needed.

And as a side note, React Context can do what Session does if you do not need reactivity