Meteor shell require cannot find module if not already imported somewhere within the app

I am trying to understand if this is expected behaviour or a bug

Let’s assume we have:

// /imports/foo.js
export const foo = 'foo';

if I want to

const { foo } = require('./imports/foo')

I get an error telling me that module ./imports/foo cannot be found

But, if somehow ./imports/foo is already imported somewhere else in the app, such that it ends up within the module import chain (final build), I can import it in the shell as well.

BTW, the same goes for the browser, which makes sense, such that unused code don’t end up at the browser.

BUT, meteor shell is a development REPL environment and it should make sense that I try out my new code in the shell before actually importing it in my app.

So, is this a bug, an expected behavior or lack of knowledge on my part?

PS: I so cannot resist pinging you here @benjamn :slight_smile:

@serkandurusoy - I just noticed an answer to your question in issue 7629:

Yep, this is expected behavior. Not that it couldn’t be improved, but the current recommendation is to explicitly import the module somewhere, e.g. if (false) require("/path/to/module.js"); so it won’t be evaluated unless you import it in the shell.

Hmm thanks @hwillson for being the nice person that you are :slight_smile: I really appreciate that you took the time to remember this thread to give me the heads up. Thanks!

PS: I’m sad that we need to work around it, though.

1 Like