Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package.json for server and client

I want to install package.json for client side from my server side package.json as the server side is using node and client side is using angular 2 directory structure

server-app

--bin

--node_modules

--package.json

--client-app

     --app

     --node_modules

     --package.json

now the problem is:

I have to run this command npm install from server app folder and also from server-app/client-app folder separately this will create deployment issues

what I want is to run only one time npm install from i.e server-app and it will automatically install the server-app package.json and client-side-app package.json too. Any help will be highly appreciated

like image 894
Kumail Hussain Avatar asked Aug 28 '17 06:08

Kumail Hussain


People also ask

Can you have more than one package JSON file?

Multiple package. json files give you a lot of flexibility to run different/incompatible versions of dependencies. As a practical example, on one of the projects that I work on we have 2 package. json files, one for the main application code and then another one for our BDD tests.

What is the package JSON file used for?

The package. json file is the heart of any Node project. It records important metadata about a project which is required before publishing to NPM, and also defines functional attributes of a project that npm uses to install dependencies, run scripts, and identify the entry point to our package.

What is peerDependencies in package json?

Peer Dependencies: In package. json file, there is an object called as peerDependencies and it consists of all the packages that are exactly required in the project or to the person who is downloading and the version numbers should also be the same. That is the reason they were named as peerDependencies.


2 Answers

Structure your application in the following way,

app
   --server-app        
   --client-app       
   --node_modules
   --package.json

This way you can have single package.json file

like image 191
Bharathvaj Ganesan Avatar answered Sep 21 '22 12:09

Bharathvaj Ganesan


I think what you need is a npm module called concurrently.

With concurrently installed in your root folder you can run multiple custom npm scripts. For example: you can create 2 separate scripts that are installing the dependencies (client-install and server-install) and then create install-all-deps script that will run both scripts one after another and install all deps in both directories.

{
    "scripts": {
        "client-install"  : "cd client && npm install",
        "server-install"  : "cd server && npm install",
        "install-all-deps": "concurrently \"npm run server-install\" \"npm run client-install\""
    }
}

Here is the npm module https://www.npmjs.com/package/concurrently. Quoting doc:

Run multiple commands concurrently. Like npm run watch-js & npm run watch-less but better.

Hope this helps.

like image 28
Nikola Jovanovic Avatar answered Sep 22 '22 12:09

Nikola Jovanovic