tinify / tinify-nodejs

Node.js client for the Tinify API.
https://tinypng.com/developers
MIT License
423 stars 74 forks source link

Cannot load cacert.pem #30

Open philiprenich opened 3 years ago

philiprenich commented 3 years ago

Hello, I am getting the following error within AWS Lambda.

2021-09-06T05:46:28.634Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"ENOENT: no such file or directory, open 'node_modules/tinify/lib/tinify/../data/cacert.pem'","code":"ENOENT","errno":-2,"syscall":"open","path":"node_modules/tinify/lib/tinify/../data/cacert.pem","stack":["Error: ENOENT: no such file or directory, open 'node_modules/tinify/lib/tinify/../data/cacert.pem'"," at Object.openSync (fs.js:462:3)"," at Object.readFileSync (fs.js:364:35)"," at eval (webpack://[*****]/./node_modules/tinify/lib/tinify/Client.js?:11:17)"," at Object.209977 (/var/task/handler.js:1:57787456)"," at __webpack_require__ (/var/task/handler.js:1:67436561)"," at ...

It appears that the cacert.pem file cannot be found and loaded. Searching around I found this SO question that seems to say that the path setup is incorrect.

Client.js doesn't use path.join() as the answer suggestions it should.

const data = fs.readFileSync(`${__dirname}/../data/cacert.pem`).toString();

My code is loading Tinify with a simple import, which seems to be the only difference, but I'm not sure it would matter?

import tinify from 'tinify'

Thank you for the help!

philiprenich commented 3 years ago

Following the route from the stack overflow answer and using path.join worked. However, this was a direct change to the compiled asset once the build was done, so is not a viable solution. Hopefully a code change here could solve it.

I'm curious why it has an issue in AWS Lambda functions and what specifically causes it because it's a showstopper that would be very noticeable if it was widespread. I'm not familiar enough with AWS's environment to know what is different about our set up.

SurfJordan commented 3 years ago

I'm having the same issue @philiprenich, I was wondering if you found a solution?

rkoopmans commented 3 years ago

The problem is indeed with the webpack output not including the cacert.pem bundle. The path.join / stackoverflow solution did not make a difference for me but it might work with a file loader of some sort. Sadly my webpack knowlege is not good enough to come up with a suggestion here.

I got it working by coping the node_modules code into my AWS functions container, but I understand that defeats the purpose of using a bundler.

An alternative solution would be to include the cacert.pem as a json array (javascript) so it will be automatically packaged, it is not very pretty but would resolve the issue.

It is important to note that the Tinify API client is not intended for use by the frontend (browser) but only in backend code.