Reputation: 831
I'm working on a simple CLI Node.js app for processing files.
App structure:
- app.js
- recipes/recipe-one.js
- recipes/recipe-two.js
Each file in /recipes folder is a function that is responsible for processing an input file. The problem that I'm facing is related to sharing dependencies.
All recipes share the same dependencies, however, I cannot wrap my head around setting those in the app.js
Whenever I do:
// app.js
const fs = require('fs');
const Recipe = require('recipes/recipe-one')
and Recipe relies on fs
- I'm getting an error saying that fs is not defined.
Question: How can I have dependencies specified in one place then? What I'm missing?
Any help would be greatly appreciated. Thank you.
Upvotes: 0
Views: 144
Reputation: 2411
I'd suggest you stick to the traditional approach of importing required modules in individual files. Still this is the possible workaround you can do to make it work.
You can use global keyword to define the modules you wanna use in multiple files. Code in 'app.js' will be:
const fs = require('fs');
global.fs = fs;
and somewhere in project you can use it like
global.fs.readFileSync() {...}
without importing/requiring it. You can read more about global here.
Hope this helps :)
Upvotes: 1