我即将开始一个具有相当大代码库的 Node.js 项目。我更愿意将我的代码与 node_modules
分开。
理想情况下,我希望使用命名空间和文件夹,因为我认为这是一种很好的管理方式。但是,假设我没看错,这意味着我必须使用文件路径导入我需要的任何文件/“类”,这将非常困惑且难以管理。
管理 Node.js 项目的大量代码的实际方法是什么?
最佳答案
我的建议是使用静态类型语言,因为它们提供有助于管理大型代码库的自动功能。例如,可以是 dart、typescript 或 coffeescript,它们都能够生成 javascript。
这篇文章很好地解释了好处(尤其是与 JS 相比): http://arstechnica.com/information-technology/2014/06/why-do-dynamic-languages-make-it-difficult-to-maintain-large-codebases/
链接文章中陈述的一些主要问题:
There is no modularization system; there are no classes, interfaces, or even namespaces. These elements are in other languages to help organize large codebases.
The inheritance system—prototype inheritance—is both weak and poorly understood. It is by no means obvious how to correctly build prototypes for deep hierarchies (a captain is a kind of pirate, a pirate is a kind of person, a person is a kind of thing...) in out-of-the-box JavaScript.
There is no encapsulation whatsoever; every property of every object is yielded up to the for-in construct, and is modifiable at will by any part of the program.
There is no way to annotate any restriction on storage; any variable may hold any value.
如果您从 JS 开始并且不想放弃当前的代码库,则可以切换到 TypeScript。 在我的 JS 项目达到 5000 行代码(大约 15 个文件)之前不久,我将其移至 typescript。我花了大约 4 个小时才让它恢复运行。
这篇文章提供了一些将 Node.js 迁移到 typescript 环境的人的见解: http://tech.kinja.com/my-experience-with-typescript-710191610
关于javascript - 在 Node.js 中管理大型代码库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21205368/