I'm creating a codebase and from this codebase I want to build several tools: a CLI, and an API, all within the one repository as a mono-repo so I don't need to publish my package. What is the project structure convention in NodeJS projects to achieve this?
My current setup looks similar to this:
myProject/
- package.json
- src/
| - index.js
- packages/
| - cli/
| - api/
The CLI and API both consume src/index.js as an example. The root level package.json contains all the dependencies.
I'm not too experienced with NodeJS, what should be the best approach here? I think I'll need to give the API and CLI their own package.json files as they're technically separate from the main project package?
Related
The situation:
client and server both share a folder shared
when we change shared in our development flow, we want the corresponding references to change in customer and server
server works because somehow with npm it seems to work, shared and server use npm
customer doesn't work and uses yarn
mixed typescript and js project
Code Structure:
root/
|- client/
|- package.json
|- src/
|- ...
|- server/
|- package.json
|- src/
|- ...
|- shared/
|- package.json // we don't want to change version every change
|- src/
|- ...
What's been tried
3 solutions proposed here
create a folder common under root and just require the files you need from your files. But you could end up with "long" require such as require("../../../../../common/file")
long require doesn't work with resolution with webpack - and isn't a nice solution
use module-alias to avoid that problem: https://github.com/ilearnio/module-alias
module-alias seems to be the same solution as the next one, in how it behaves.
you could make common a local module (using file:) and install it in package.json https://docs.npmjs.com/files/package.json#local-paths
we currently do this, but we still have to reinstall the shared folder on a change, we currently use yarn upgrade shared which takes a long time and still requires us to know when we need to run it.
in addition we've attempted to get yarn link working but doesn't seem to link properly
we've attempted to use a post-pull hook with husky to run yarn upgrade shared but it's too slow to run every pull especially since it's not needed often
we've considered a true mono-repo 'package' like Lerna but still don't think that it's necessary for the cost to migrate
link-module-alias to sym link folders on post install script, but this fails with typescript files
Goal
either
find a way to automatically sync these in dev environement
find a solution that installs/updates manually - but is fast and can be run on every pull
find a way to automate running yarn upgrade shared that runs (roughly) only when needed not all the time
I guess we could find a way to automate the version increment on any change of shared's version key, and then it's tracked, we could run yarn install and that work work.
The find solution was that we were using React-Native and therefore the normal steps for syncing would work for the IDE but not the React-Native app.
Here is a great article describing how to get metro bundler working with it - however for Typescript we added it to the TSConfig and for the IDE we still needed to add it to our package using the file:../shared directive.
Recently I've done a few small projects with a Node backend and a React frontend using create-react-app. Although I've spent quite a bit of time researching best practices for folder structures I haven't been able to come up with a satisfying solution without having to eject the react app. I wonder if there are solutions out there that I just haven't found yet.
The folder structure I am trying to aim for looks something like this:
package.json
src/
client/
app.js
...
server/
index.js (node.js main file, could just as well be in server/)
This layout would allow me to run all scripts from the root folder and I can put all configurations in one package.json. For example, I would like to be able to just run npm test from the root folder which would then use jest to run both the client and the server tests.
As far as I can tell create-react-app is hardwired to expect a src/index.js when using npm start, which prevents me from using this folder structure - if I just move all the boilerplate generated by create-react-app into the client folder I have an ugly src/client/src folder which again contains it's own package.json. I guess I could split it differently, creating a client/src and server/src, but I still have separate package.json files in each directory.
Would love the hear about your experiences with this, maybe I am missing the obvious solution.
mainDir/
app/
src/
index.js
package.json
api/
src/
index.js
package.json
I follow this pattern for developing applications in microservice architecture.
This structure is for mono-repos.
You also can divide the front end and backend into different repositories.
I want to dockerize a REST Api which uses multiple microservices. Most tutorials only cover very simple cases so I am a bit stuck about how to properly dockerize such a project.
My project structure:
node_modules
src
request-manager
request-worker
rest-api
shared
test
package.json
tsconfig.json
So it has three microservices (request-worker, request-manager and the rest-api).
My question:
Is a .dockerignore and Dockerfile for each microservice necessary or how would one properly dockerize such a mono repository project?
You can create one Dockerfile per microservice, but will probably have several conflicts and problems due to the fact that they are sharing multiple files (node_modules especialy).
I recommand use one (sub)repository for each service:
request-manager
Dockerfile
src
node_modules
test
package.json
tsconfig.json
request-worker
Dockerfile
src
node_modules
...
rest-api
...
shared
And be very careful at what you put in shared folder.
You also can use docker-compose for managing dependancies.
I have an app using the file structure
package.json
src/main/
- webapp
- java (containing my API)
My API is restricted so that it can only dispatch files from within src/main/webapp, as a result I need my node_modules to install within that folder.
If I run npm install I get the following:
package.json
node_modules/
src/main/
- webapp
- java (containing my API)
Is there a way I can change this so it installs by default in the following structure?
package.json
src/main/
- webapp
- node_modules/
- java (containing my API)
If possible I'd rather not have a global environment variable as I work on multiple apps on this machine and I don't want them all installing to this apps node_modules
For the case above I intended to install my webapp dependencies, such as AngularJS using NodeJS. It turns out there is another package manager Bower that is much more fit for this purpose and allows you to configure the location of its bower_componentsto wherever you like. This has solved my issue and I now have the following file structure
package.json
node_modules/
src/main/
- webapp
-bower_components
-angular (etc)
- java (containing my API)
I have a project in which I use node-webkit. node-webkit allows npm packages to be used for developing desktop applications. I make use of grunt to build my application.
My folder structure looks like this at the moment:
project root
node_modules/ (1)
package.json (1)
App/
node_modules/ (2)
package.json (2)
bower.json
bower_components/
...
controllers/
filters/
...
app.js
The npm dependencies for the application itself are kept within the App folder, but the dev dependencies for building the project are not related to the application source code, so i keep them in node_modules (1) inside the root folder. I also know that in a package.json file one can express dependencies and dev dependencies, exactly for this reason. I would rather have one package.json file in the root expressing ALL dependencies, including dev dependencies, but i would rather have a separation of those dependencies on folder level.
Two questions arise:
Is this a good way to organize my npm dependencies? If yes, awesome? If no, which I expect:
What is a better way to organize my dependencies? Is it possible to specify that dev dependencies go into folder a, and 'regular' dependencies go into folder b? If so, how do I do this?
In case anyone is wondering, this is the project i am talking about:
https://github.com/michahell/pinbored-webkit
[updated folder structure to include app.js for clarity]
It is perfectly fine to keep more than one package.json file and multiple node_module directories for a project. If you consider the parts as separate components.
An example might be if, you have one directory containing a node server, another containing a react app, and a third containing some kind of deployment script written in javascript.
#Michael package.json file contains all the dependencies related to that project.There is no need for multiple package files and multiple node_modules folders..
But you need to check where is your App.js file!!
your App.js , package.json must be in same folder unless configured.