Package.json for server and client - node.js

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

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.

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

Related

npm ci crashes with local packages

I'm using nodejs 10 and npm 6.9.
I wanted to create two projects. One dependant from the other.
So I created these folders
-myapps
---parentproj
---childproj
then I did these steps:
go into parentproj folder
execute "npm create"
execute "npm install fs-extra" (for adding a third party reference)
go into childproj folder
execute "npm create"
execute "npm install ..\parentproj"
now the childproj folder contains both the package.json and package-lock.json file.
If I run "npm ci" I get this error
"npm ERR! fs-extra not accessible from parentproj"
Moreover, if I run "npm ls" from childproj folder I get this message
`-- UNMET DEPENDENCY fs-extra#^7.0.1
Am I doing something wrong?
Which is the correct way for working with local packages without publishing them?
regards.

Create single binary from express + react app with zeit pkg?

How do I package an express + react folderstructure into a single binary with zeit?
I can run each of them like this:
node server.js
npm run start client
I can start both like this:
scripts element in package.json:
"myapp": "concurrently --kill-others \"node server\" \"npm run --prefix client start\"",
then - npm run myapp in the same folder as package.json.
What I would like to achieve is to apply zeit/pkg somehow so that I have a single binary that I can run that starts both servers in the same way as npm run myapp.
Anyone who knows how?
I don't think zeit/pkg accepts multiple entry points based on their documentation
The entrypoint of your project is a mandatory CLI argument. It may be:
Path to entry file. Suppose it is /path/app.js, then packaged app will work the same way as node /path/app.js
Path to package.json. Pkg will follow bin property of the specified package.json and use it as entry file.
Path to directory. Pkg will look for package.json in the specified directory. See above.
Maybe the better route would be to do some server-side rendering via their Next.js framework? Then you would only have to package your app via the NodeJS entry point.

grunt build nodejs - multiple projects

I'am searching for a good way for building a multi project application.
At the moment I have this structure:
Every app is a nodejs application
- parent folder (git root)
|- app1
|-- app1-backend
|-- app1-frontend
|- app2
|- app3
at the moment i need to install every app by hand with the following steps:
install npm modules with npm install
install typings with typings install
compile app with tsc
every app folder contains the following: (app1-backend, app1-frontend, app2, app3)
tsconfig.json, package.json, typings.json
should i automate that with grunt?
Should I use a own grunt file for each project?
greets
Since it's already 3 self contained commands, you can probably get by with just adding a script in the package.json of each project that handles all it's building commands, ie:
{
"name": "project-name",
...
"scripts": {
"build": "npm install && typings install && tsc"
}
}
Which will allow you to just run npm run build to run all 3 commands for any given project.
Then you can just run
(cd /path/to/project1 && npm run build) & (cd /path/to/project2 && npm run build) & (cd /path/to/project3 && npm run build)
Which will build all 3 simultaneously.
Note: I'm assuming npm will handle multiple processes, but you may have to run sequentially
It is possible to use grunt to run whatever shell commands such as using grunt-shell; however for me personally, it doesn't make sense to have a build process in one project that will cause another project to build.

Heroku build for node app with unusual folder structure

The project is divided into a backend code and react native client code.
Both are sharing one github project. It looks like this:
backend/
--- src/
--- package.json
client/
--- src/
--- package.json
For my heroku instance, I want to run only the backend code, but at the same time want to use continues integration feature from github.
Is there a way to make heroku run npm install & start only from the backend folder?
Create a package.json in the root of the whole project (the parent dir of backend). You can do this with npm init --yes.
Give that top-level package.json file two scripts:
"scripts": {
"postinstall": "cd backend && npm install",
"start": "cd backend && npm start"
}
Should do the trick.

Run npm from subdirectories on Heroku

I have a project which contains 2 subprojects:
First is the API
Second is the client
And both of these projects have their own dependencies mapped in their own packages.json files, which is placed in each subdir.
So the question is how to run npm install from sub directories on heroku?
I tried putting something like this in the main npm file
"scripts": {
"postinstall": "cd my_subdir; npm install"
}
But it doesn't work, showing can't cd to my_subdir
Utilize npm's --prefix option:
"scripts": {
"postinstall": "npm install --prefix ./my_subdir"
}

Resources