1

I'm trying to pass a json file path to an ebay auth function to gain a token. This works locally by stating the file name alone. However, as I have my next js app deployed as Node in Vercel, I need to call readFileSync to ensure the json file is included in the build.

To do that i've implemented: -

const { readFileSync } = require("fs");
  var path = require("path");

  const file = readFileSync(
    path.join(__dirname, "config/eBayJson.json"),
    "utf8"
  );

in my getServerSideProps.

However, although my file is clearly at the right path, and I'm calling __dirname to ensure I have the right path. I'm still getting errors, now locally and in the vercel deployment that the file or directory does not exist: -

Error: ENOENT: no such file or directory, open '\config\eBayJson.json'
    at Object.openSync (node:fs:490:3)
    at readFileSync (node:fs:391:35)
    at getServerSideProps (D:\Web\StoreApp\nextjs-store\.next\server\pages\inventory.js:3846:16)
    at renderToHTML (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\render.js:40:221)        
    at async D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:112:97
    at async D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:105:142
    at async DevServer.renderToHTMLWithComponents (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:137:387)
    at async DevServer.renderToHTML (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:138:522)
    at async DevServer.renderToHTML (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\server\next-dev-server.js:35:578)
    at async DevServer.render (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:75:236)
    at async Object.fn (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:59:580)    at async Router.execute (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\router.js:25:67) 
    at async DevServer.run (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:69:1042)
    at async DevServer.handleRequest (D:\Web\StoreApp\nextjs-store\node_modules\next\dist\next-server\server\next-server.js:34:504) {
  errno: -4058,
  syscall: 'open',
  path: '\\config\\eBayJson.json'
}

enter image description here

I'm at a loss for what I'm doing wrong. Most of the stack overflow answers I could find were about the lack of __dirname which I've already included.

Any help much appreciated!

Simon Edge
  • 141
  • 2
  • 10

1 Answers1

7

SOLVED

Thanks to the help from @juliomalves and this thread: - https://stackoverflow.com/a/65861629/1870780, solved my problem with the updated code:

    export async function getServerSideProps(req, res) {
      const { readFileSync } = require("fs");
      var path = require("path");
      const configDirectory = path.resolve(process.cwd(), "config");
      const file = readFileSync(
        path.join(configDirectory, "eBayJson.json"),
        "utf8"
      );
    
      const EbayAuthToken = require("ebay-oauth-nodejs-client");
      const ebayAuthToken = new EbayAuthToken({
        filePath: path.join(configDirectory, "eBayJson.json"),
        // input file path.
      });
grreeenn
  • 2,287
  • 1
  • 20
  • 27
Simon Edge
  • 141
  • 2
  • 10