How to load a dependency via variable with require


#1

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'

#2

requires of dynamic sources are not supported.

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


#3

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?


#4

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


#5

Yup i’m using 1.3 with imports.


#6

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


#7

@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’) .


#8

Thanks a lot! Will try this later!


#9

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