简体   繁体   中英

Managing a large codebase in Node.js

I am about to embark on a Node.js project with a fairly large codebase. I would prefer to keep my code separate from node_modules .

I would ideally like to work with namespaces and folders as I think it would be a nice way to manage things. However, assuming I have read correctly, this would mean that I would have to import any files/"classes" I require using the path to the file which would be quite messy and hard to manage.

What is the defacto method for managing a large amount/ of code for a Node.js project?

My advice is to use a static typed language, since they provide automatic functionality which helps managing large codebases. That could for example be dart, typescript or coffeescript, all being able to produce javascript.

This post explains well the benefits (especially compared to JS): http://arstechnica.com/information-technology/2014/06/why-do-dynamic-languages-make-it-difficult-to-maintain-large-codebases/

Some of the main problems stated in the linked article:

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.

If you started with JS and don't want to abandon your current code base, you could switch to typescript. Shortly before my JS project reached 5000 lines of code (in about 15 files), I moved it to typescript. It took me about 4 hours to get it back to running.

This post gives some insights from someone movig Node.js to a typescript environment: http://tech.kinja.com/my-experience-with-typescript-710191610

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM