Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing package.json dependencies in universal/isomorphic apps

With React and other frameworks it is now common to use npm and package.json to install the libraries you'll use on the frontend. If you are developing a universal/isomorphic app, this introduces the problem that the dependencies for the frontend and backend are stored in the same file, creating a massive dependency list.

If you use npm --save/--save-dev both types of dependencies (frontend, backend) become mixed and it's difficult to know, without going one by one, which one is used where.

Other than sorting and managing the dependency list by hand, is there any way to keep the list tidy? What are your strategies to manage dependency lists?

like image 225
JayC Avatar asked Apr 07 '16 19:04

JayC


People also ask

Does order of dependencies matter in package json?

json just lists dependencies in alphabetical order by default, so I don't think it matters. You usually won't be manually adding the dependencies to your file, rather you'll be instructing npm to save packages as dependencies when you install them.

What is the difference between devDependencies and dependencies in package json?

"dependencies" : Packages required by your application in production. "devDependencies" : Packages that are only needed for local development and testing.

How do I change the dependencies in package json?

For updating a new and major version of the packages, you must install the npm-check-updates package globally. It will display the new dependencies in the current directory whereas running this command will list all the global packages which have new releases.

How do I add packages to devDependencies?

If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'. This will add your desired npm library to the package. json file.


1 Answers

In a universal/isomorphic app, you'll probably have few dependencies that are purely frontend or purely backend; most of the dependencies are shared.

An option that comes to my mind is to use more than one package.json:

  • one for isomorphic dependencies (react, redux, etc.) and utility dependencies like the build system (babel, webpack, etc.), task runners (gulp, cross-env), testing (karma) and so on;
  • one for pure frontend dependencies;
  • one for pure backend dependencies.

I haven't used that structure myself, and I'm not sure that it will be more maintainable that a single package.json, because you'll have to manage more things (and if you add npm-shrinkwrap to that, it's twice as more files).

like image 134
sompylasar Avatar answered Nov 10 '22 00:11

sompylasar