问题
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:
How would I go about referencing these dependencies in the plugin's package.json?
Is there any better solution than running npm install every prestart?
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).
回答1:
U can use nodemon to restart the server as soon as anything changes
npm install -g nodemon
http://nodemon.io/
来源:https://stackoverflow.com/questions/35416149/node-js-manage-frequently-changing-local-dependencies