Member-only story
How to import a module from the root of a Node.js project
Using native absolute modules specifiers relative to the package root instead of double dots, transpilers or bundlers
I am payed for the task completion, not for the hours I play. Unless you are a consultant, you are in the same situation. To complete the task, such as migration of something to something else, I need to create an auxiliary Node.js-based command-line tool that is unlikely to be reused again. The tool should be flawless before its only use, but providing for potential future problems would be a pure waste of time. So I am always interested in the most straightforward solutions.
For some reasons, I really need to import a module saved in the project root into a module saved deep in the project folder tree. For example, let’s figure out the best way to import root.js into file3.js:
I can think of four possible solutions. According to the simplicity and durability, the first is ok. The two last options are equally the best. The second, the most popular, in my case would be a waste of time.
Relative URL
I could easily import a file from the package root into file3.js using a relative URL with several double dots: