0

I am trying to import images I have stored in a folder called resources located under the server section.

When I place the images in the app folder on my local drive, it works fine, but since this has to be deployed to our live server, it makes things hard to have to copy files back and forth.

The error I get:

Error: ENOENT: no such file or directory, open '../../resources/cp_pdf_logo.png' at Object.openSync (node:fs:585:3) at Object.readFileSync (node:fs:453:35) at RouteEmails.orderAuth (C:\Development\kaizen\server\routes\emails\file:\C:\Development\kaizen\server\routes\emails\func.js:187:43) at RouteEmails.sendMail (C:\Development\kaizen\server\routes\emails\file:\C:\Development\kaizen\server\routes\emails\func.js:430:17) at RouteOrders.orderAuthorizedMail (C:\Development\kaizen\server\routes\orders\file:\C:\Development\kaizen\server\routes\orders\func.js:840:24) at processTicksAndRejections (node:internal/process/task_queues:96:5) at RequestRouter._routeRequest (C:\Development\kaizen\server\socketController\file:\C:\Development\kaizen\server\socketController\requestRouter.ts:64:34) at RequestRouter.processMessage (C:\Development\kaizen\server\socketController\file:\C:\Development\kaizen\server\socketController\requestRouter.ts:43:9) at Socket. (C:\Development\kaizen\server\socketController\file:\C:\Development\kaizen\server\socketController\socketClient.ts:62:7) { errno: -4058, syscall: 'open', code: 'ENOENT', path: '../../resources/cp_pdf_logo.png' }

Here is an example of my code:

let ordercplogo = this.base64_encode("../../resources/cp_pdf_logo.png") let orderthankyoulogo = this.base64_encode("../../resources/thankyou_logo.png") // let ordercplogo = this.base64_encode("/app/resources/cp_pdf_logo.png"); // let orderthankyoulogo = this.base64_encode("/app/resources/thankyou_logo.png"); 

The commented-out section is what works, but from the image below you can see that I have these files stored inside the app.

enter image description here

Can anyone tell me what I'm doing wrong?

PS. I have also tried to import the images above with

const logo1 = require("../../resources/image.png") 

but also doesn't work and throws another error.

1
  • print the current working directory, more than likely your app isn't running where you think it is and you are a directory level off in your code. console.log(process.cwd()) Commented Jul 18, 2022 at 14:52

1 Answer 1

1

Your function probably tries to find the image relative to the working directory. It makes a difference if you were to do cd /; node /app/index.js or cd /app; node index.js. The working directory may be different in your live environment than locally.

There are a number of functions to help you find the path to a file in the built-in path module.

Let's assume you're in a file called routes/companies/a.js:

const { resolve } = require('path'); this.base64_encode(resolve(__dirname, "../../resources/cp_pdf_logo.png")); // Will find the absolute path to the file. 

( path.relative documentation page )

In CommonJS files (files that use require()) the value __filename always refers to the file it's in, rather than the main script file (so /app/routes/companies.a.js in this example) and __dirname is the directory it's in. That's the same value, without the a.js part.

Sign up to request clarification or add additional context in comments.

1 Comment

You could also solve this by putting the file path resolution in the base64_encode function. It takes multiple paths so you could do resolve(__dirname, '../../', file_requested_by_user).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.