The application compiled using pkg in Node.js does not recognize the files added to the project folder. What should I do?

147 Views Asked by At

I need to be able to add new .js files to the /public/bots folder after compiling my project into an exe file using the pkg module. Upon launching the application, these new files should be detected, and specific functions should be imported from them. Could you please advise if this is achievable, and if so, how?

**Here is the code I am using. ** However, after compiling, Node.js does not recognize the added files in /public/bots

const directoryPath = path.join(__dirname, 'public/bots');

fs.readdir(directoryPath, (err, files) => {
    if (err) {
        console.error('reading eror', err);
        return;
    }

    console.log('files list:');
    files.forEach(file => {
        console.log(file);
        const filePath = directoryPath + '/' + file
        console.log(filePath)
        const module = require(filePath)
    });
});

When I run the project compilation using the code provided above, I receive the following warning:

Warning Cannot resolve 'filePath'
  C:\Users\Serg\Desktop\BOT\app.js
  Dynamic require may fail at run time, because the requested file
  is unknown at compilation time and not included into executable.
  Use a string literal as an argument for 'require', or leave it
  as is and specify the resolved file name in 'scripts' option.
0

There are 0 best solutions below