How to load a dependency via variable with require

I want to do something like this:

var route = '.' + route.path + '-view.js';
require(route);

But it doesn’t work. This is working.

require('./search-view.js');

So for some reason it’s not working with variables. Is this a meteor issue or a require issue? I’ve tried various solutions found on the internet and a few of my own without any success.

When doing the first example I am getting this:

Cannot find module './search-view.js'

requires of dynamic sources are not supported.

I solve this by appending a string to a specific import file, then the client reload itself.

I don’t think I can follow what you mean by appending a string to a specific import file. Could you please give an example?

Are you using 1.3? Do you have an import folder?

Yup i’m using 1.3 with imports.

If you want to use import feature or require. Your files must be placed inside imports folder.

1 Like

@AndreasGalster I assume that you are running require during run-time [inside a callback function] since you are using route path. Its different when you add import to a callback instead of adding directly to a file which runs. It loses context. Here you have to give full path to the file from root. For eg {a} = require(’/imports/ui/views/search-view.js’) .

1 Like

Thanks a lot! Will try this later!

After many tests conclusion is it’s not working with a dynamic require. I’ve filed an issue here: