-1

Scenario

So, I have initialised a Node project with npm. I have setup my package.json accordingly. I am using typescript, so I have also setup tsconfig.json.

I have a few dependencies (npm packages) that I will need to use multiple times in multiple files of my project. index.ts is the root of my project. I can import these libraries in index.js and other files also.

Problem

Is there any way to include or import these libraries in the project only once so that I can use them in any file of the project without having to import a single thing anywhere.

I tried searching for various ways to do this using - CommonJS module syntax, NodeJS module syntax, global modules - but none of it can provide me the way I want it.

For Ex -

Most of the answers or solutions I got were like this

  • Export all the libraries through a single file

    import abc from 'abc';
    import xyz from 'xyz';
    
    module.exports = {
        abc, xyz
    };
    
  • Use these libraries in other files like

    import modules from 'src/modules.ts'
    
    var wantSome = modules.abc.getSome();
    

But, this still has an overhead of importing modules file and accessing it like modules.abc.

Do we have any way to make these imports available globally through out the project.

P.S. - This scenario is somewhat similar to ngModules in Angular 2+, where we can import whatever we want inside ngModules and it is then available to all the components under that module.

Onkar Joshi
  • 27
  • 2
  • 7
  • The example you've shown is the best according to what you want. Why cany you use it that way? The other option would be to store them in the Global variable but that wouldnt be appropriate. Or better yet, just import each file when needed. Nodejs wont re-import the library if you had already imported it, it will use the cached value – Nelson Owalo Oct 22 '18 at 07:16
  • I don't want to use it because, I see Angular's ngModule has that functionality which I need. So, there must be some way to implement it in normal node application using maybe CommonJS module patterns or something like that! And I already mentioned the overhead because of this way. I want to eliminate that! – Onkar Joshi Oct 22 '18 at 07:17
  • There might be a library like that, but thats a wrong design for your nodejs app – Nelson Owalo Oct 22 '18 at 07:19
  • Is there any reason why you want to use import how its used in angular? like performance? – Nelson Owalo Oct 22 '18 at 07:20
  • Ease of use! That's all. Just specify what you want to use in your project and don't worry about importing it in lots of files with same import statement! – Onkar Joshi Oct 22 '18 at 07:22
  • you can import all the libraries in your entry file `index.js`? and store the references in the global variable – Nelson Owalo Oct 22 '18 at 07:25
  • Then can I use them from all other files too? – Onkar Joshi Oct 22 '18 at 07:27
  • yes, you can use them in all the othe files – Nelson Owalo Oct 22 '18 at 07:27
  • Can you tell me how to declare globally? – Onkar Joshi Oct 22 '18 at 07:28
  • Check the answer – Nelson Owalo Oct 22 '18 at 07:31

3 Answers3

3

Doing this is going to cause you all sorts of problems in the long run.

  • you'll end up with one monolithic file containing all your exports, which will become onerous to maintain
  • if you decide to modularise your code, it will be more difficult since you won't explicitly know which modules a file uses
  • if you were able to include to the point where you simply use the modules with no reference, code clarity will suffer
  • name clashes might occur

The best I believe you'll get in node is to declare all the includes in a single module, and then destructure within your own file.

import abc from 'abc';
import xyz from 'xyz';

module.exports = {
    abc, xyz
};

Then

import modules from 'src/modules.ts'
{ abc: { getSome } } = modules

But I strongly suggest using the standard patterns for importing. It will make your code much cleaner and easier to maintain

sofcal
  • 490
  • 3
  • 7
1

N/B - Thats a bad way to design your app.


In node, you can set global variables via the "global" or "GLOBAL" object:

You could import all your dependencies in your app entry file, and store them in a Global variable

index.js

import abc from 'abc';
import xyz from 'xyz';

global.abc = abc
global.xyz = xyz

someotherfile.js

//access abc import
global.abc
Nelson Owalo
  • 2,324
  • 18
  • 37
0

Export all the libraries through a single file

import abc from 'abc';
import xyz from 'xyz';

module.exports = {
    abc, xyz
};

Use these libraries in other files like

import {abc, xyz} from 'src/modules.ts'

var wantSome = abc.getSome();