Making the file location insensitive to where the node command is being run from? - node.js

The script src/main/js/script.js loads a file with a relative location of ../json/file.json If I run node src/main/js/script.js then I get an error saying that the ../json/file.json cannot be resolved, however if I cd into the directory src/main/js and run node script.js from there, there's not problem. How can script.js be written such that it does not matter where the script is being run from. In other words how do I specify that the file.json file is located relative to the location of the script.js file?

var location = path.join(__dirname, '../json/file.json');
require('fs').createReadStream(location)
Originally it was:
require('fs').createReadStream('../json/file.json');

Related

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.

Getting a directory outside symbolic links when running script nodejs

Let's assume the following example
ROOT
config
my.json
releases
today
index.js
current (symlink to today for example)
i would like to include the json file in my index.js file using a simple requirestatement, require('my.json')
The nodejs script would be launcher using node index.js or node fullpath/index.js
The problem encountered is that the require path will always resolve to releases/today/index.js instead of current/index.js. Because of this the inclusion of my json file is not correct.
The following techniques have been tried, followed by the output:
__dirname: /ROOT/releases/today
process.cwd(): /ROOT/releases/today
process.env.PWD: depending on the startup location of the script: / or /ROOT/current
require('path').resolve(__dirname)): /ROOT/releases/today
process.env.PWD doesn't always give stable results and i was wondering if there is something that has the same result.

Local node process running in wrong directory

I am using PHP storm 9 to run a node instance on my local dev environment., but the process seems to be running in server.js parent directory.
My folder structure looks like
app/
app_server/
server.js
user_resources/
user_resources/
When i write a file with in my local instance to writes user_resources in app/ and when i run the same process on live environment it writes to user_resources in app_server/
pdf.create(html, options).toFile(path+filename, function (err, reuslt) {
callback();
});
using fs writeFile, readFile or readdir gives similar behavior
Local node server i ran with PHPstorm and live server runs with forever
Both local and live is a ubuntu system.
Any suggestings to why local node seems to be running in server.js parent directory.
The node server is probably executed from the app directory by PHPStorm, while the live process runs from app/app_server.
If no other hint is provided in the server code on where exactly to put the user_resources it will reside within the current working directory (which is the path from where the node process was involved).
You may want to specify a path relative to the location of the server.js, this can easily be done like this:
var userResourcePath = __dirname + '/user_resources`;
Node always ensures the __dirname to be set to the path of the file it is in.
I made the assumption the user_resources path of your live environment (app/app_server/user_resources) is the one you want for local development.

Node.js - res.sendFile - Error: ENOENT, stat - Even though path is correct

Here's my folder structure :
Root Folder
...
...
...
app.js
escape_iframe.html
...
from within app.js I'm doing :
res.sendFile('escape_iframe.html' , {root : __dirname});
and the error I'm getting is :
Error: ENOENT, stat '/app/escape_iframe.html'
which I guess is a horrible way of saying that the file can't be found in the path provided ?
Anyway, as you can tell I simply want to serve a file that is a sibling of app.js (which is the file form which the res.sendFile(... call is being made)
I also tried : res.sendFile('escape_iframe.html'); and I got : path must be absolute or specify root to res.sendFile
What am I doing wrong ?
Paths specified with a . are relative to the current working directory, not relative to the script file. So the file might be found if you run node app.js but not if you run node folder/app.js.
To make a path relative to the script, you must use the __dirname variable.
res.sendFile(__dirname + '/path/to/escape_iframe.html');
Path specified with . are relative to the path and any number of forward slashes are truncated as single slash. You might get this error when the process node filename.js is not able to locate the filename.
https://nodejs.org/api/errors.html#errors_enoent_no_such_file_or_directory
My suggestion would be ::
var path = require('path')
//process.cwd() returns the current directory of the project
res.sendFile(process.cwd(),'/path/to/escape_iframe.html');

Node JS newbie: why do I get a 404? (angular-seed)

I'm trying NodeJS for the first time.
I'm following the hello world instructions at https://github.com/angular/angular-seed and https://github.com/thedigitalself/angular-sprout (which has the same hello world instructions for a slightly modified fork of the first one).
In both cases, I'm supposed to just run a web-server.js file that is included in the repo using Node and then navigate to http://localhost:<port>/app/index.html
This gives me a 404 error page (which is written in the web-server.js file), as does just /app/.
But if I navigate to http://localhost:<port> I get a directory listing for the filesystem directory where the web-server.js script is, which is also written in web-server.js.
Any idea why web-server.js cannot find /app/ or /app/index.html?
Make sure you run the server from your root directory.
~/angular-seed/
In here you do node scripts/web-server.js
Then you can visit
http://localhost:<PORT>/app/index.html

Resources