Home > Articles > Web Development

  • Print
  • + Share This
Like this article? We recommend


This article showed you how to handle file paths in Node. The path() module provides the file path handling methods and properties. You can find the current absolute path of the file name and directory name using the _filename and _dirname properties.

Likewise, to find the current working directory, you can use the process.cwd() method; to change directories, you can use the process.chdir() method. If you need to find the path to the node executable, use the process.execPath() method.

The path.sep property makes sure that you can parse or combine paths using the correct syntax for path separators, which is determined by the operating system on which the Node application is running.

The delimiter property makes sure that you can parse or combine path variables using the right path delimiters.

To get a file extension, simply use the path.extName() method; to get the file name, use the path.baseName() method. If you need only the file name and not the extension, use the baseName() method, but also pass it the extension name to exclude. To return only the directory portion of a path, use the path.dirname() method. If you need to normalize a path, use the path.normalize() method. And finally, if you need to find the relative path between two directories, use the path.relative() method.

I recommend becoming familiar with these methods and properties by setting up a test environment and using the code examples above and/or extending them yourself to make sure you get a good grip on handling file paths, which are important to most applications.

  • + Share This
  • 🔖 Save To Your Account