Skip to content
Advertisement

Import JSON inside NPM Module

I made an npm module, this module export a function that load a json file and then export the result ( a little bit simplified )

The probleme is when I import this module inside another project I have this error :

JavaScript

I looks like when I import my module, it try to read the json inside the current directory and not inside the npm module.

The code inside my module :

JavaScript

Advertisement

Answer

Final answer (for ES Module) :

JavaScript

If you don’t use ES Module (but commonJS), __dirname already exist so you can do :

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement