How to get the path to the current script with Node.js?

Sometimes, we want to get the path to the current script with Node.js.

In this article, we’ll look at how to get the path to the current script with Node.js.

How to get the path to the current script with Node.js?

To get the path to the current script with Node.js, we can use __dirname .

__dirname returns a string with the path of the current script.

For ES nmodules, we can use import.meta.url instead.

Conclusion

To get the path to the current script with Node.js, we can use __dirname .