pug.compileFile('./views/transactions/html.pug'); Heroku Node Express. Error: ENOENT: no such file or directory, open - node.js

Error: ENOENT: no such file or directory, open './views/transactions/html.pug'
Running my application locally I don't have a problem, once I push to Heroku I get the above error. Locally I am using nodemon to serve my express server. I'm using pug to compile some html to send emails. This is the function that is causing the problem.
pug.compileFile('./views/transactions/html.pug')
Using node Express, with React, serving the static/build files from the same server.
My suspicion is that I need to serve the 'views' directory as when I am running locally my nodemon will be doing this, however, I 'm not sure how to do this.
This is a view of my server.js file.

I have a solution to the error message. I got this from this post about node/heroku filepaths.
The issue was that locally my file path was pointing to the correct file but the online/heroku server needed a different filepath. I have included a picture of the file structure for what it's worth.
function createEmailFilePath(filename) {
const newpath = process.env.NODE_ENV === 'production' ?
path.resolve('emails', 'transactions', filename) :
path.resolve('..', 'emails', 'transactions', filename);
return newpath;
}

Related

How to use docx npm package in production to save a file

I am generating a doc file using docx-template npm package in node js. and the file is getting successfully saved in my backend/controller folder on my local machine. Now i want to do prod deployment on Heroku but i dont know what path has to be set to save the file in production.
I have used 'fs' module to read and write file. Shown below.
fs.writeFileSync(
path.resolve(__dirname, `${contractName} ${element.frequency}.docx`),
buffer
);
You probably have to use the "send(Buffer)" feature of express.
See the following page :
https://expressjs.com/en/api.html#res.send
const contentTypes = {
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
};
res.set('Content-Type', contentTypes.docx);
res.send(buffer);
This is because the "fs" module is used to write the data on the disk, but if it is in production, most likely what you want is to let the user "download" a file, and that is done using res.send() which takes as an argument a Buffer to achieve this feature.

File path not found when code is deployed on a remote server

I have the following directory structure for my node/express app:
/
/client_side_code
- object00.png
/server_side_code
- server.js
When running a local server on port 3000, my server.js script accesses object00.png successfully with the following code:
var pathToImage = __dirname + "/../client_side_code/original_object_images";
fs.readdirSync(pathToImage).forEach(function(file,e) {
console.log(file);
});
Now, when I upload my application to a remote server (Heroku), the console says that this path can't be found. Specifically, I get the following error message in the console:
Error: ENOENT: no such file or directory, scandir '/app/server_side_code/../client_side_code/original_object_images
As you can see, the object00.png image can no longer be found. I thought using __dirname would also work on a remote server since it begins a relative path. Any thoughts as how I can allow this image to be accessed when it's hosted on a remote server?

Serving static files with Express.js on heroku. Where do files reside in Heroku?

The issue is Heroku can not find the files of my node project. This prompts the 404 Not found error. The question is how does the generic node Heroku file structure look like? If so, where do the files of my project actually reside in the heroku?
The scenario:
With the express library, I put all files needed to serve a frontend template, in a folder called 'public'. The absolute path leading to 'public' is needed and'_dirname' is the variable that contains that.
app.use(express.static(_dirname+'/public');
Now, Heroku always makes a GET request to the root of the application. As a response I send the
index.html file as the starting template. I also provide its absolute path as '{root:_dirname}'.
app.get('/', function(req, res) {
res.sendFile('public/index.html'{root:_dirname});
});
When the server is run, I get a 404 Not found error. Which means Heroku cannot find the files.
Heroku root folder is named app/. So as expected, providing the local '_dirname' is wrong since the application is now running on Heroku server not locally.
2020-09-10T15:28:12.127567+00:00 app[web.1]: Error: ENOENT: no such file or directory, stat '/app/C:/Users/...
But, pointing at the public folder alone(only /public), assuming app/ is root, still prompts the 404 error not found.
2020-09-10T15:31:23.630358+00:00 app[web.1]: Error: ENOENT: no such file or directory, stat '/index.html'
The Question
Which leads to the question, what does heroku file structure look like? where do project files reside in the heroku file structure, so i can provide the correct path on the GET request?
My file structure
After Bergur suggestion:
It prompts a 404 Not found. However, the path is now theoretically correct.
This is what I changed:
app.use(express.static(path.join(__dirname+'/public')));
app.get('/', function(req, res) {
res.sendFile('index.html',{root:__dirname+'/public'});
});
The 404 error comes from the 'app.get('/,function(req,res){});'. Without this handler, my heroku application shows a 'cannot GET' message on the template.
Have you tried to log out the full path just to see if there's something off?
I have several heroku projects with nodejs and react/vue/angular dist folder without problems.
I would change your code a little bit to:
use nodes __dirname
Use path.join instead of inserting the '/' yourself. It might cause some problems.
So final version might look like:
app.use(express.static(path.join(__dirname, 'public')));
This assumes the project structure is
-index.js
-public/index.html
There should be no need to use sendFile, serving the public folder is enough. Just make sure that the public folder is being deployed/pushed to heroku.
If you share with us your folder/project structure we might help you you better.

Heroku Error: ENOENT: no such file or directory, stat '/app/index.html'

I deployed my nodejs app to Heroku
My project folder structure is as below
folder structure
Server.js
Server.js code
After deployment I am getting above error as no such file or directory /app/index.html
I tried running run bash command to check folder structure on Heroku but it doesnt show me app folder at root path
Heroku run bash output
wondering why I can't see app folder above where it is coming from?
Appreciate your help.
Your index.html has a caps, you called it Index.html.
If you are using windows just notice you might have to delete Index.html, then push your project without it, then push it again with your file correctly named (index.html).
You are not seeing app/ because you are inside of it.

nodejitsu 400 Error: ENOENT, open '/opt/run/snapshot/package/images/tmp/72118-89rld0.png

I'm using nodejitsu to deploy a simple image upload program with express. In my code I've changed the default upload directory by following command
app.use(express.bodyParser({
uploadDir: __dirname + "/images/tmp"
}));
It's working fine on my localhost but when I'm using nodejitsu I'm getting this error
400 Error: ENOENT, open '/opt/run/snapshot/package/images/tmp/72118-89rld0.png.
Can anybody tell me how to make it work on nodejitsu as well? Actually I'm new to node as well as nodejitsu.
I had the same problem. Try to check directory at application start:
var fs = require('fs'),
upload = __dirname + "/images/tmp";
fs.exists(upload, function (exist) {
if (!exist) {
fs.mkdir(upload);
}
});
It was helpful for me, may be it would helpful for you.
make sure that directory /opt/run/snapshot/package/images/tmp/ exists. Otherways just mkdir those directory
At first check that the directory exist or not. If not then create it and follow the command
sudo jitsu deploy
I think the problem will be solved.

Resources