0

I have a vanilla js project that I am building with parcel. In project I have a directory with a lot of json files that I would like to use in one of my modules. I have tried to import them like I would import js files but that didn't work.

import * as regions from './Polygons'

How could I import all the files and iterate over them like you would iterate over an array of objects?

Leff
  • 1,968
  • 24
  • 97
  • 201

1 Answers1

0

import only works if the thing you are importing is an ES6 module. Just use require in a try-catch:

import fs
dir = './Polygons'

allFiles = fs.readdirSync(dir).map (filename) ->
  try
    json = require(dir + '/' + filename)
  catch e
    error = e
  return { filename, json, error }
user2926055
  • 1,963
  • 11
  • 10