16

I'm new to Node, coming from a Java background. These days I'm experimenting with each part of a full application: database, rest api, ui.

So far I wrote the database-backed logic, which runs on its own, processes text files, store data about them in the database and exposes a REST API to query that data. I'm now going to make the ui to navigate that data.

Would it be reasonable having a structure like this:

- (a) main project folder
    - (b) backend application (a Restify server responds to REST calls querying the database)
    - (c) ui application (an http server serves React static files)

If that makes sense, I would guess that:

  • (b) has a package.json with server- and rest- related dependencies (i.e. Restify, MongoDB, ...)
  • (c) has another package.json with dependencies for ui (i.e. React, Webpack, etc, but not Restify or MongoDB)
  • (a) has a third package.json which cares for installing each sub-project (I'd say by running npm install through hand-written npm-scripts). Otherwise, how do you usually handle such Node projects? Do you keep each application completely separate from the rest?

For those who know that tool, this mimics a Maven multi-module project; though that level of automation is not needed, I'd just like to come up with a self-contained package.

watery
  • 5,026
  • 9
  • 52
  • 92
  • Thanks @watery for this excellent question! Can you share your end result? – barfuin Jul 29 '19 at 18:52
  • 1
    @barfuin As usually happens with commercial projects, I've soon been set on another one, so I hadn't had a chance to try anything :-) But I'll report here as soon as I can find something. – watery Jul 29 '19 at 22:01
  • 1
    @barfuin I started a new project where I'm extracting parts of an existing monolithic application into separate packages (ui components, icons, etc), as subfolders beside the main application folder; Lerna helps use them as local dependencies (though it may feel a bit strange at times, i.e. you can only add one dependency at a time, there's no uninstall command, but npm uninstall breaks because local dependencies are declared as regular ones); there's no support though for application packaging / deploying AFAICT - and it is said in several issues that Lerna is not a deployment tool. – watery Apr 09 '20 at 23:24

1 Answers1

11

These project structures are called as monorepos - A single node project repository that contains multiple packages. There are tools like Lerna. If you are using yarn as package manager, it comes with experimental feature of workspaces.

barfuin
  • 16,865
  • 10
  • 85
  • 132
PKV
  • 424
  • 4
  • 6
  • Thank you. I'm currently using webpack, but I'll have a look at both. – watery Apr 20 '18 at 10:52
  • Back after some Lerna experience. You may find my impressions in my comment to my own question. It seems Lerna only covers half of my requirements, offering no packaging / deployments facilities; but then even a tool like Gulp (for what I've seen) doesn't provide any kind of project structure / lifecycle (a la Maven so to speak), in fact I ended up writing my own copy-this-zip-that-upload-those-there script. – watery May 01 '20 at 16:05