Exploring various ways to load files using
Versions used below
If you’ve used node before these are familiar.
Load one of node’s built-in modules.
Dependency from npm
Load something that’s in
node_modules/, which was most likely installed via npm.
Load a file relative to the current file.
The file extension is optional.
I’ve never used this but it’s possible to load files with an absolute path.
Again, the file extension is optional.
Given this directory structure
├── stuff/ │ ├── foo.js │ ├── bar.js │ └── index.js └── main.js
main.js you could
and it’d load
index.js can require the other files in
stuff/. This is handy for making a directory sort of act as an embedded module.
One benefit to omitting the file extension in relative path requires is that you can break a large file down into a directory of smaller files without having to update all the
When you require a json file you get the parse result.
Any file from a dependency
If you’re okay with relying on a module’s internal file structure, you can require any file from it.
This allows you to programmatically determine a module’s version.