3

I'm currently working on my first larger node.js application which should work as modular as possible using plugin-like dependencies. Because I'm in the early stages of development and am also quite new to node, this involves a lot of trial and error hence I need to restart the application a lot. Therefor the start time should be as short as possible.

In my case I'm having a file structure like this

/lib - core functionality
/plugins - local modules, will be moved to external packages later on
/plugins/project-users
/plugins/project-post
/plugins/project-forum

Since I want to move these plugins to their own package and include them with require('project-users') once things start working, I have to install these correctly.

npm install ./plugins/project-users
npm install ./plugins/project-post
npm install ./plugins/project-forum

So far everything is working fine but I will have to reinstall these modules everytime I make changes to them (since this is very often at the beginning, I'm using scripts/prestart). I also tried using symlinks with the fs core-module which appearantly doesn't work on USB flash drives (at least I couldn't get it to work).

The problem now is that these plugins depend on each other

  • project-forum depends on project-post and project-user
  • project-post depends on project-user

To sum it all up, there are some questions coming into my mind:

  1. How would I go about referencing these dependencies in the plugin's package.json?

  2. Is there any better solution than running npm install every prestart?

  3. Also, how can I make sure there is just on instance of project-user installed? Running npm dedupe everytime seems a bit to much (obviously depending on the answer to 1.).

Maybe I'm just thinking to complicated for this or I'm not familiar enough with how node.js and npm are supposed to work. Please tell me if this is the case. If something isn't clearly described, feel free to ask.

UPDATE: I'm currently leaving out the dependencies between my plugin completly and loading all of them to my "core"-Object ({users: require('project-users'), post: require('project-post'), forum: require('project-forum')}). I will then have to check manually if the module is loaded (hence the object key set). This still doesn't seem like the smartest solution to me, but for the moment it seems to work.

The only thing that really bothers me is that I have to install the local modules every time I change any code of my modules (currently just reinstalling all of them at app start).

PostCrafter
  • 655
  • 5
  • 15
  • I would suggest doing a simple `require('./plugin/whatever')` on your modules for now, instead of installing them and to do `require('whatever')`, which requires calling install all the time just as you said after changes are made. Just start simple. Once they are acual external packages or in code freeze, you can still change your `require` statements. For early development on both the main application _and_ dependencies this is (to me at least) super unpractical, as you already noticed by yourself. If your code and structure is clean that little bit of refactoring should be no problem. – Num Lock Mar 10 '16 at 11:52
  • Thanks for your reply. How do you think about the "core"-object I have? Is this a good idea to keep the require-paths as absolute as possible, only using relative path in a inter-module context in one place? Or would you use the local path to the local modules at every file I have to actually use them? Coming from a java background this seems a bit ugly to me. – PostCrafter Mar 10 '16 at 11:58

1 Answers1

-5

U can use nodemon to restart the server as soon as anything changes

npm install -g nodemon

http://nodemon.io/

mikefrey
  • 4,653
  • 3
  • 29
  • 40
Chetan Kumar
  • 397
  • 3
  • 13