0

Hi there I was wondering if it's possible to do imports inside a for loop. When trying to execute this code:

const fs = require('fs');
function importFiles(x) {
  fs.readdir(x, (err, files) => {
    files.forEach(file => {
        var doc = file
        var char = /.+(?=\.\w+)/.exec(file);
        import {doc} from str(x + char);
    });
  })
}
importFiles('../public/img/');

I get SyntaxError: Unexpected token import. Obviously this might be wishful thinking (since I cannot find any documentation on this) but I was wondering if I could somehow decide on how much to import within a function.

Micks Ketches
  • 456
  • 1
  • 4
  • 14
  • `import`, by rule, has to be at the top of your script. Inside of a function, you have to use the `require` syntax. – iamjpg Jan 19 '17 at 02:24
  • `require({char})(str(x + doc));` states that path must be a string. Is there a proper way to word this? – Micks Ketches Jan 19 '17 at 02:38
  • Take a look at the proposed [import()](https://github.com/tc39/proposal-dynamic-import) syntax - it allows for this kind of implementation – CodingIntrigue Jan 19 '17 at 09:53

0 Answers0