Search code examples
javascriptnode.jsmodulepathrequire

Least verbose way of requiring a single file?


I'm using Node.js and want to split my code into separate files. However, it seems quite verbose to require these modules in a reliable way.

For example:

var module = require(path.resolve(__dirname, "./file.js"));

Furthermore, if the file is in a different folder, I'll need to deal with relative path mayhem.

It seems like quite a simple problem, but my research hasn't yielded a solution. Thanks for you time.


Solution

  • Require paths are always relative to the script being run, so you don't need path.resolve or __dirname.

    There are a number of solutions for avoiding the inconvenience of relative paths.

    Thanks cdhowie for your comment.