Clarification of /server folders in 1.3 suggested /import structure

Hey all,

I was wondering if I could get some clarification on this section of the Guide.

Can I assume that any files residing in a server/ or client/ folder within the /imports folder (or anywhere for that matter) will behave exactly as if they were in the root of the project?

So for example, this file: /imports/api/bikes/server/s3-upload.js will be server-only?

Cheers.

Third edit, my apologies :slight_smile: Yes, you are correct.

1 Like

Hmm - @gadicc, I don’t think is quite the case. Anything in server/ should be server side only. Here’s a quick way to verify this:

a) meteor create client-server-test
b) Create imports/client/client_test.js as:

const clientTest = {
  sayHello() {
    console.log('Hello from the client!');
  }
};

export default clientTest;

c) Create imports/server/server_test.js as:

const serverTest = {
  sayHello() {
    console.log('Hello from the server!');
  }
};

export default serverTest;

d) Replace /client/main.js contents with:

import clientTest from '../imports/client/client_test.js';
import serverTest from '../imports/server/server_test.js';

clientTest.sayHello();
serverTest.sayHello();

e) Replace /server/main.js contents with:

import { Meteor } from 'meteor/meteor';
import serverTest from '../imports/server/server_test.js';

f) Wipe the contents of /client/main.html.
g) Access the app - you will see:
Uncaught Error: Cannot find module '…/imports/server/server_test.js’

To verify why, take a look behind the scenes:

a) Client Side:

  • Crack open [app_root]/.meteor/local/build/programs/web.browser/app/app.js
  • Search for serverTest; it won’t exist meaning it can’t be referenced client side.

b) Server Side:

  • Crack open [app_root]/.meteor/local/build/programs/server/app/app.js
  • Search for serverTest; it will exist meaning it can be referenced server side.
1 Like

@hwillson, great detective work! Thanks for the correction. So the rule seems to be, you can import client files from the server but not vice versa, I’ll amend my answer.

1 Like

Thanks @gadicc - I believe importing client files from the server (if the client files are under client/) is also prohibited. I haven’t really dug through the meteor build tool source for this, but I believe the client/ and server/ restrictions are in effect both ways (where client/ based code is stripped from the server build, and server/ based code is stripped from the client build). I just tested my example above with the following addition:

/server/main.js:

import { Meteor } from 'meteor/meteor';
import serverTest from '../imports/server/server_test.js';
import clientTest from '../imports/client/client_test.js';

and the build fails with ***Error: Cannot find module ‘…/imports/client/client_test.js’***, so that seems to be the case.

Actually, I did try to take a quick look at the meteor build tool source, but quickly realized I haven’t had nearly enough coffee yet today to try to make sense of it … :slight_smile:

2 Likes

Thanks for the fantastic and thorough answers guys. Really appreciate Meteor’s community for small, but important questions like these!

1 Like

@hwillson, thanks a 2nd time. You’re right. I was referring to a behaviour that was present in one of the betas, but was reverted for 1.3 final. Thanks for setting me straight :slight_smile:

1 Like

How this worked was not clear to me from the Meteor Guide so I created this pull request that has been accepted.

1 Like

@skirunman, great job! Thank you.