Next.js Discord

Discord Forum

Can't see some files in subdirectory after deploying to Vercel

Answered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
Hello everyone! I'm trying to write the UI for trading bot. I have the repository of the bot (written in node.js) and I placed the build of that bot into my Next.js project into public folder. I was able to run the bot locally from the UI by calling my app's API route. Here is the code snippet of the API handler:
export default function handler(req, res) {
  if (req.method === 'GET') {
    botProcess = spawn('node', [filePath, botConfig]);
  } else if (req.method === 'DELETE') {
    * handling of stop of bot process *
  }
}


While it's working perfectly fine in my local environment, when I deploy my project to Vercel, I'm getting this error when trying to start the bot:
node:internal/modules/cjs/loader:1148
throw err;
^
Error: Cannot find module '/var/task/public/bot/strats/geckoMarketMaking.js'
Require stack:
- /var/task/public/bot/strats/main.js
}
Node.js v20.14.0


It says that it can't find the geckoMarketMaking file inside strats directory. However, on Source tab of my deployed commit in Vercel I can see that all files in bot directory are present (attached image). Same is in the project repo, files are there (attached image). However, when I'm trying to debug the pathing issue in my bot/strats/main.js, I have next:
const path = require('path');
const fs = require('fs');
const dirPath = path.join(process.cwd());
const testPath = path.join(process.cwd(), '/public/bot');
console.log('dirPath:', dirPath, 'testPath:', testPath);
try {
    const files = fs.readdirSync(testPath);
    console.log('Directory contents:', files);
} catch (err) {
}


The outputs of logs are:
"dirPath: /var/task"
"testPath: /var/task/public/bot"
"Directory contents: [ 'package.json', 'strats' ]"

which means that it doesn't see any other files except main.js in strats folder and package.json.
I couldn't find any similar issue or solution of how to fix it. Will be appreciated for any kind of help on this. Thanks!
Answered by Asian black bear
Okay, I have fixed it by adding this to next.config.mjs:
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    outputFileTracingIncludes: {
      '/api/runBot': ['./public/bot/**/*'],
    },
  },
};

export default nextConfig;
View full answer

3 Replies

Asian black bearOP
Asian black bearOP
Okay, I have fixed it by adding this to next.config.mjs:
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    outputFileTracingIncludes: {
      '/api/runBot': ['./public/bot/**/*'],
    },
  },
};

export default nextConfig;
Answer