node.js管理频繁更改的本地依赖项 [英] node.js manage frequently changing local dependencies

查看:148
本文介绍了node.js管理频繁更改的本地依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发我的第一个更大的node.js应用程序,该应用程序应该使用类似插件的依赖项来尽可能地模块化. 因为我还处于开发的早期阶段,并且对节点还很陌生,所以这涉及很多试验和错误,因此我需要大量重新启动应用程序.因此开始时间应尽可能短.

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

由于我想将这些插件移到它们自己的软件包中,并在开始工作后将它们包含在require('project-users')中,所以我必须正确安装它们.

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

到目前为止,一切都工作正常,但是每次对它们进行更改时,我都必须重新安装这些模块(因为这在开始时就很常见,因此我正在使用脚本/预启动). 我还尝试过将符号链接与fs核心模块一起使用,该符号链接显然不适用于USB闪存驱动器(至少我无法使其正常工作).

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-post取决于项目用户

总而言之,我想到了一些问题:

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

  1. 我将如何在插件的package.json中引用这些依赖项?

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

有没有比每个prestart都运行npm install更好的解决方案?

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

此外,如何确保仅在项目用户实例上安装了?每次运行npm重复数据删除似乎有点太多(显然取决于对1的回答).

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.).

也许我只是想为此变得复杂,或者我对node.js和npm应该如何工作还不太熟悉.如果是这种情况,请告诉我. 如果没有明确描述,请随时询问.

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.

更新:我目前正在完全保留插件之间的依赖关系,并将所有依赖关系都加载到我的核心" -Object({users: require('project-users'), post: require('project-post'), forum: require('project-forum')})中. 然后,我将不得不手动检查模块是否已加载(因此设置了对象密钥). 对于我来说,这似乎并不是最聪明的解决方案,但目前看来,它是可行的.

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).

推荐答案

一旦发生任何更改,您可以使用nodemon重新启动服务器

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

npm install -g nodemon

http://nodemon.io/

这篇关于node.js管理频繁更改的本地依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆