Getting a directory outside symbolic links when running script nodejs - node.js

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.

Related

How to resolve path to a file within node_modules

Problem loading a file using relative path when my node app is initialised using a another node app
I have created an npm which relies on a file stored relative to project root. something like this
index.js
- res
- config.json
Now I read the config.json using following code
const pathToConfig = path.resolve(__dirname, '../res/config.json')
This works great locally.
But in my prod setup this app is initialised by another node app.
And __dirname resolves to root of that app so all my logic to find config.json get messed up.
Is there any way I can read the file without worrying about how node app was initialised?
Have you tried the command process.cwd()? It is almost the same as __dirname but does differ slightly.

NodeJS/CloudFoundry - failed: The app upload is invalid: Symlink(s) point outside of root folder

I'm testing CloudFoundry on IBM and are running NodeJS.
When trying to cf push my application I get the following error:
failed: The app upload is invalid: Symlink(s) point outside of root folder
In my appllcation I have the following code:
return res.sendFile(path.join(__dirname +'/tvshows/'+ guide +'.html'));
When not using path.join and simply use:
return res.sendFile(path.join('./tvshows/'+ guide +'.html'));
I get this error instead:
TypeError: path must be absolute or specify root to res.sendFile
What to do?
I've also tried stuff like path.join((process.env.BUILD_DIR || __dirname), and return res.sendFile('index.html', { root: path.join(__dirname, 'tvshows', guide) }); but no luck.
The fail came from my node_modules folder.
Adding .cfignore with node_modules/ fixed the issue.
You didn't mention the version of the cf cli that you're using, but I think that this is expected behavior starting with version 6.34.0.
push now preserves relative symlinks in app files. This makes it easier to work with Node.js apps using npm link, but note that it now errors when it detects a symlink pointing outside the app folder (e.g. a node_modules folder containing external symlinks).
https://github.com/cloudfoundry/cli/releases/tag/v6.34.0
I think you're running into the second part, "how errors when it detects a symlink pointing outside the app folder". It's nothing to do with the code in your app, but rather somewhere in your project folder there is a symlink which references another file that is not under the root of your project.
If you're on a unix-like system you can run find . -type l to find all the symlinks under the current directory. Then you just need to figure out which one is pointing outside of the project root.
Options are to remove that symlink, point it to something under your project root or use .cfignore to ignore that file (which is what you ended up doing).
Hope that helps!

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

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');

Unexpected token import - using react and node

I'm getting an error when running my react app: Uncaught SyntaxError: Unexpected token import
I know that there are a plethora of similar issues on here, but I think mine is a little different. First of all, here is the repository, since I'm not sure where exactly the error is: repo
I'm using create-react-app, and in a seperate backend directory I'm using babel (with a .babelrc file containing the preset es2015). The app worked fine until I added another file in a new directory in the backend folder (/backend/shared/validations/signup.js).
I was using es6 before that too, and it was working perfectly fine. First I thought it was some problem with windows, but I cloned the repo on my Ubuntu laptop and I'm getting the same error there.
Some things I tried already:
Move the entire folder from /backend to the root folder
Move just the file (signup.js) just about everywhere
So no matter where the file is, the error stays the same. If I remove the entire file and all references to it the app works again.
I think this error is pretty weird, considering I'm using es6 everywhere else in the app without trouble. It would be great if anyone could help me with this error.
edit: If you want to test this on your own machine just clone the repo and run npm start in the root folder (and also npm start in the backend folder, but that isn't required for the app to run, or for the error to show up).
That's what's happening:
Your webpack is set to run babel loader only in the src folder (include directive).
To change that, one approach is:
1) extract webpack configuration with npm run eject (don't know if there is another way to override settings in react-create-app). From the docs:
Running npm run eject copies all the configuration files and the
transitive dependencies (Webpack, Babel, ESLint, etc) right into your
project so you have full control over them.
2) in config/paths.js, add an appShared key, like that:
module.exports = {
/* ... */
appSrc: resolveApp('src'),
appShared: resolveApp('backend/shared'),
/* ... */
};
3) in config/webpack.config.dev.js, add the path to the babel loader, like that:
{
test: /\.(js|jsx)$/,
include: [ paths.appSrc, paths.appShared ],
loader: 'babel',
/* ... */
},
It works now!

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