''require.context' is not returning file names
I am working on a NextJS app and have created a few functions that facilitate a user uploading files. Files uploaded by the user will live in the "../public/uploads" directory until my team is ready to move forward with an external file storage service like AWS S3.
After retrieving these files and their contents, I have noticed that the object being returned from require.context does not contain the file's name, and instead refers to it as "Module".
Is there any way to get the name of a file retrieved through require.context? It could be that my logic is faulty; I just want a second opinion:
const getExistingDocs = (uploadPath) => {
function importAll(r) {
return r.keys().map(r);
}
const userTXTFiles = importAll(
require.context('raw-loader!../public/uploads/', true, /\.txt$/, 'sync')
);
var docs = new Array();
userTXTFiles.forEach(function (file) {
docs.push({
name: file,
path: uploadPath + file,
preview: file['default'],
size: file['default'].length,
});
});
return docs;
};
var existingDocsJson = getExistingDocs(uploadDirectory);
console.log(existingDocsJson);
The value I get for file
looks like: {name: Module{__esModule: true, default: "Text File Contents..."}}
I have used this site for reference: https://webpack.js.org/guides/dependency-management/ Please let me know if more context is needed to answer this question!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|