When I try to run my js code there appears to be an error that shouldn’t exist no matter what.
Basically i loop through an array of strings (paths), i try to import an object (using require) using each path in the array but it gives the error:
TypeError [ERR_INVALID_ARG_VALUE]: The argument 'id' must be a non-empty string. Received ''
I tried logging the array to the console right before the forof block got executed and all of the elements were non-empty strings. I also tried using a foreach loop on the array but it gave the same error.
const filePaths = [
"./file1.js",
"./file2.js",
"./file3.js",
"./file4.js",
];
// each file exports an object
filePaths.forEach((filePath) => {
const fileObject = require(filePath);
console.log(fileObject);
});
This code will result in that TypeError [ERR_INVALID_ARG_VALUE]
From what i could tell by the console logs, it seems that the error is thrown on the last element, no matter the length. In the example it’s "./file4.js"
which is still confusing because its not an empty string like the error says.
And yes, i am sure the error is coming from inside that for loop
Bogdan Maxim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.