I am working on a nuxt.js project and getting error:
In browser I am seeing this error:
__webpack_require__(...).context is not a function And, in terminal I am getting this error:
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted Here is my code
<script> export default { name: 'SectionOurClients', data() { return { imageDir: '../assets/images/clients/', images: {}, }; }, mounted() { this.importAll(require.context(this.imageDir, true, /\.png$/)); }, methods: { importAll(r) { console.log(r) }, }, }; </script> I have used the above script from HERE.
Please help, thanks.
EDIT: After following @MaxSinev's answer, here is how my working code looks:
<template lang="pug"> .row .col(v-for="client in images") img(:src="client.pathLong") </template> <script> export default { name: 'SectionOurClients', data() { return { images: [], }; }, mounted() { this.importAll(require.context('../assets/images/clients/', true, /\.png$/)); }, methods: { importAll(r) { r.keys().forEach(key => (this.images.push({ pathLong: r(key), pathShort: key }))); }, }, }; </script>
importinstead ofrequire?importandrequireimports file as module, if you want to import images you can do that only with specified webpack loader and there is no difference betweenimportandrequirefor webpack. But in question example we can not useimportbecause we have to use webpack helperrequire.contextto load all images dymically during bundling..png's can be importedimport defaultAvatar from '@/assets/images/default-avatar.png'