Book Image

NW.js Essentials

Book Image

NW.js Essentials

Overview of this book

Table of Contents (17 chapters)
NW.js Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Handling paths in NW.js


At first, handling paths in NW.js can be quite tricky. The first time you might find yourself in trouble is when requiring a custom module not stored in the default node_modules folder. Let's say, for example, this is our folder structure:

Application root
- data/myData.json
- js/script.js
- modules/myModule.js
- modules/otherModule.js
- index.html
- package.json

When requiring a module from the window context, the module's path is treated as relative to the application's root directory, so in our example, whether you're calling it from index.html or js/script.js, we would proceed as shown in the following code:

require('./modules/myModule');

When calling a module from another module, however, you should use the relative path from the current file. So, let's say we're requiring otherModule.js from myModule.js, which is in the same folder:

require('./otherModule');

The next thing you might worry about is how to access files for I/O operations from both contexts using their...