Home > Mobile >  How does Nodejs handle relative paths?
How does Nodejs handle relative paths?

Time:08-13

I have a question regarding Nodejs's path-handling. I know that they have a page about that in the docs but it didnt contain what I needed.

So basically, I have a file that includes a relative path referencing a file (png in this case).

Now, based on where I call the file from, the picture is either found or not found (as the point in the fileSystem from where its called changes).

I am using the 'sharp' framework, 'sharp('./picture.png')' is similar to require.

Example:

File 'render.js' :
const pic = sharp('./picture.png')

Calling:

cmd\examplePath> node render.js //picture is found

cmd> node ./examplePath/render.js //picture is not found

The location of the picture relative to the file stays the same at all times!

My question now is if what I have described is to be expected from Nodejs or if there is something wrong. What would I need to do to be able to call the file from anywhere and have it still work?

Any tips are appreciated. Thanks :)

CodePudding user response:

Normally file handling in nodejs such a fs.open() just resolves a relative path versus the current working directory in nodejs. The current working directory will be whatever the OS working directory was when you started your nodejs app.

The current working directory is not necessarily the same as the directory where your script is located because the current working directory might be different than where your script is located when your nodejs program was started.

So, in your two command line examples, each is starting with a different current working directory, thus one works and one doesn't.

In general in nodejs, it is not advisable to rely on what the current working directory is because this lessens the ability to reuse your code in other projects where it might be loaded from a different directory. So, if the file you are trying to reference is in a known location relative to your script's file system location, then you would typically build a full path, using either __dirname (in CommonJS modules) or import.meta.url or import.meta.resolve() (in ESM modules).

For example, if the image is in the same directory as your script, then you could do this in a CommonJS module:

const path = require('path');

const fullPath = path.join(__dirname, "picture.png");
const pic = sharp(fullPath);

In an ESM module, you could do:

import path from 'path';

const __dirname = new URL('.', import.meta.url).pathname;
const fullPath = path.join(__dirname, "picture.png");
const pic = sharp(fullPath);

My question now is if what I have described is to be expected from Nodejs or if there is something wrong.

Yes, that is to be expected. Your are starting your program with different current working directories and have code that depends upon the current working directory being a certain value (because of your usage of a relative path).

What would I need to do to be able to call the file from anywhere and have it still work?

Build an absolute path using your script's directory as the anchor location as shown above.


Note that require() or import use completely different file searching logic and have some built-in behavior that is relative to the module's location that is running them. But, they are the exception, not the rule. Operations in the fs module (or other modules that use the fs module) use the current working directory as the base path if you supply a relative path.

  • Related