Skip to content

Instantly share code, notes, and snippets.

@danieltorscho
Created August 7, 2019 09:39
Show Gist options
  • Save danieltorscho/9e86e5f3f0aa137f751c47e3e0d4ed4e to your computer and use it in GitHub Desktop.
Save danieltorscho/9e86e5f3f0aa137f751c47e3e0d4ed4e to your computer and use it in GitHub Desktop.
Check module existence within Front end
/**
* Check if required module exists physically
*
* Checking if a requested filename is existing physically
* without having to load it before for performance purpose.
* This check should be made before requiring the script.
*
* @param {*} filename Name of the file to check for
*/
moduleExists (filename) {
// Search for .js files inside a specific folder
let req = require.context('./drivers', false, /\.js$/i)
// Get their cleaned up filepath names
const files = req.keys().map(item => item.slice(2, (item.length - 3)))
// Return results as an array or an object
return files.includes(filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment