Meteor Guide: File Structure question

Reading the Meteor Guide, I’m confused by this paragraph:


To fully use the module system and ensure that our code only runs when we ask it to, we recommend that all of your application code should be placed inside the imports/ directory. This means that the Meteor build system will only bundle and include that file if it is referenced from another file using an import (also called “lazy evaluation or loading”).

Meteor will load all files outside of any directory named imports/ in the application using the default file load order rules (also called “eager evaluation or loading”). It is recommended that you create exactly two eagerly loaded files, client/main.js and server/main.js, in order to define explicit entry points for both the client and the server. Meteor ensures that any file in any directory named server/ will only be available on the server, and likewise for files in any directory named client/. This also precludes trying to import a file to be used on the server from any directory named client/ even if it is nested in an imports/ directory and vice versa for importing client files from server/.

These main.js files won’t do anything themselves, but they should import some startup modules which will run immediately, on client and server respectively, when the app loads. These modules should do any configuration necessary for the packages you are using in your app, and import the rest of your app’s code.


Doesn’t this mean for example that if there is a file inside the [/app-name/imports/server] directory, this file can NOT be imported in the client here [/app-name/client/main.js]?


For example I could NOT do the following:

Module inside the imports /server directory:

Module inside the imports /client directory:


Entry point in Meteor client:

// => Would NOT work?
import { ServerTest } from "../../imports/server/server-test.js"; 

// => Would work?
import { ClientTest } from "../../imports/client/client-test.js"; 

Yep, you’re right. If need more info, take a look at this code repos

And for secure methods, take a look at this repo specially the /imports/modules folder

Sorry if that language is confusing (as I wrote it), but it looks like you got your answer below. To reiterate, any code located in a /client or /server directory that is located inside an /imports directory still has special meaning to the Meteor build tool and can only be loaded to the client or server respectively.

1 Like

Thank you, your wording here is crystal clear.

Server code is only available on the server, and client is only available on the client. The DDP connection that is built into meteor is what connects the client and server.

If you want something to be available on both, define it within the lib directory, which is available on both client and server.