How to run a Node.Js backend project from another developer? - node.js

I'm new to Nodejs, normally when I make a project, I install the dependencies myself, and in order to run the project set something like this in the packaje.json:
"scripts": {
"dev": "nodemon server.js"
}
and then I just run my project with something like this npm run dev
But in a project made by someone else it doesn't seem to be that simple because it has the dependencies like this:
{
"name": "proyecto API",
"version": "1.0.0",
"description": "proyecto Main Backend Repo",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cross-env NODE_ENV=production node --harmony index.js",
"dev": "cross-env NODE_ENV=development nodemon --harmony index.js",
"lint": "eslint --ext .js --ignore-path .gitignore .",
"lint:fix": "eslint --ext .js . --fix",
"superuser": "node --harmony createSuperUser.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/proyecto-Network/lolagato"
},
"author": "gatito",
"license": "MIT",
"bugs": {
"url": "https://github.com/proyecto-Network/lolagato/issues"
},
"homepage": "https://github.com/proyecto-Network/lolagato/erths",
"dependencies": {
"#google-cloud/storage": "^5.14.2",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongo-project": "^1.0.1",
"mongodb": "^4.1.0",
"morgan": "^1.10.0"
},
"devDependencies": {
"cross-env": "^7.0.3",
"nodemon": "^2.0.12"
}
}
Apparently here I can't just do npm run dev because as you can see in the packaje.json it comes out this:
"dev": "cross-env NODE_ENV=development nodemon --harmony index.js"
So my questions are, how can I run this project on my PC and what does that say "cross-env" and --harmony mean?
I have to install all that that appears in the packaje.json?

Just do,
npm install
This will install all the required dependencies. If you notice, in the devDependencies there is cross-env which is required for your npm run dev.
Once the dependencies are brought up, do npm run dev.
Or you could just do -
nodemon index.js
or
nodemon --harmony index.js
To see what happens.
cross-env NODE_ENV=development is just setting the environment variables. From their npm page -
cross-env makes it so you can have a single command without worrying about setting or using the environment variable properly for the platform.

Related

nodemon: not found on heroku

My app runs locally with no issues by its failing on Heroku
package.json
{
"name": "app name",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon app.js"
},
"engines": {
"node": "14.17.0"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.12.9"
}
}
Procfile
web:node app.js
its returning the below error
sh: 1: nodemon: not found
Any help on how to make this work?
The nodemon is installed globally on your machine that's why you are able to run it on your machine. When you are pushing it on Heroku it is showing the error because there is nothing like nodemon.
Also, Heroku doesn't need nodemon so just replace your start script:
node app.js
and change your Procfile to this:
web: npm start
Even I had the same issue.
You just have to remove nodemon from start script
It has to be iin this way;
"scripts": { "start": "src/index.js", "dev": "env-cmd -f ./config/dev.env nodemon src/index.js" }

How can I deploy a react app with a node backend on GitHub Pages?

I just started learning about concurrently npm to React.
I want to know how to deploy React project on Github page with concurrently npm.
Normally on local machine we would run the app with server side package.json file
"dev": "concurrently "npm run server" "npm run client""
In Terminal> npm run dev
I tried with gh-pages npm to deploy react app on Github page but with concurrently npm,
I have no idea how to do it since there are two package.json.
Also, I don't know much about NODE environment and npm run build.
Server side package.json:
{
"name": "contact-keeper",
"version": "1.0.0",
"description": "Contact manager app",
"main": "server.js",
"homepage": "https://myprofile.github.io/Contact-Keeper-with-React",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"clientinstall": "npm install --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"config": "^3.1.0",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"express-validator": "^6.1.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.6.4"
},
"devDependencies": {
"concurrently": "^4.1.1",
"gh-pages": "^2.1.1",
"nodemon": "^1.19.1"
}
}
Client side server package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"react-transition-group": "^4.2.1",
"uuid": "^3.3.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5000"
}
Is there any document or basic knowledge about setting environment that I need to learn more to understand this topic?
Github is not (quite) a service for serving your working application to other users. Github is a repository for storing your code, updating it, and managing versions.
I think that to deploy your app "concurrently" to github means there is a way of updating Github with the new version code, at the same time as you deploy it online to a service, such as Heroku.
You should research and understand what Git (as opposed to github) is, as its essential for development. Get skilled at managing your app code with git first, before trying to deploy to a service like Heroku, AWS etc....
EDIT
As pointed out by Asaf Aviv, you can serve front-end apps from Github, with github pages, but you still need to be able to push your local code up to github for this to work.

build before restarting the node.js (express) server using Nodemon

I am using nodemon to watch the changes to the server files and restart the server. It works fine when the script is ec5
nodemon ./server.js
but I want to write the script in ec6 and compile using babel doing the same thing but building before the server is restarted.
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel src --out-dir dist",
"serve": "npm run build && node dist/index.js",
"serve-dev": "nodemon dist/index.js"
},
"dependencies": {
"babel-preset-env": "^1.7.0",
"express": "^4.16.4"
},
"devDependencies": {
"#babel/cli": "^7.0.0-rc.1",
"#babel/core": "^7.0.0-rc.1",
"#babel/node": "^7.0.0-rc.1",
"#babel/preset-env": "^7.0.0-rc.1",
"nodemon": "^1.18.3"
}
Run with babel-node. (I can see it's already installed as dev dependency)
"serve-dev": "nodemon --exec babel-node src/index.js"
(assumed that src/index.js is your entry point of your app)
I think nodemon has built-in support for this now.
Create a config file: https://github.com/remy/nodemon#config-files
Watch for the restart event and put your build script stuff there: https://github.com/remy/nodemon#triggering-events-when-nodemon-state-changes

Trying to run nodemon wih concurrently

Hey i'm trying to run nodemon to perform the command npm start which uses concurrently to open 3 nodejs processes
this is my package.json
{
"name": "web",
"version": "1.0.0",
"description": "Classic website",
"main": "index.js",
"scripts": {
"site": "node site/index.js",
"socket": "node socket/index.js",
"bots": "node bots/index.js",
"start": "concurrent \"npm run site\" \"npm run socket\" \"npm run bots\" "
},
"keywords": [
"gsa",
"web"
],
"author": "sas",
"license": "ISC",
"dependencies": {
"child_process": "^1.0.2",
"concurrently": "^3.5.1",
"express": "^4.16.2",
"express-handlebars": "^3.0.0"
}
}
However when i run nodemon i get an error -
Error: Cannot find module 'D:\skindole\index.js'
What command exactly are you entering when you run nodemon? I think perhaps if you remove "main": "index.js", from the package.json you may have success.
I think what you want to do is in the start scripts for each of the site, socket, and bots replace node with nodemon.
I ran into the same issue with concurrently, however, adding --raw param to concurrently did a trick for me.
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"wave": "concurrently --raw \"npm run client\" \"npm run server \""
},
Try using && instead of concurrent?
"start": "npm run site && npm run socket && npm run bots"

Deploying ember-cli app to Heroku

What do I put in my Procfile to deploy an ember-cli generated app to Heroku?
=== web (1X): `ember server`
web.1: crashed 2014/04/10 13:19:57 (~ 48s ago)
=== web (1X): `npm start`
web.1: crashed 2014/04/10 13:22:11 (~ 2m ago)
You could use the Heroku Ember CLI buildpack: https://github.com/heroku/heroku-buildpack-emberjs
I have this in my Procfile and it's is working for me:
web: ember serve --environment production --port $PORT
I followed the instructions from this gist, which is where I got that Procfile.
The ember-cli version and dependencies in the gist are out of date, which was causing my app to crash similar to yours.
I needed to add/replace the packages listed in the "dependencies" section of my package.json file with the packages ember-cli puts into the "devDependencies" section of package json. When I was done my package.json file looked like this:
{
"name": "your-apps-name",
"version": "0.0.0",
"private": true,
"directories": {
"doc": "doc",
"test": "test"
},
"scripts": {
"start": "ember server",
"build": "ember build",
"test": "ember test",
"postinstall": "bower install"
},
"repository": "https://github.com/stefanpenner/ember-cli",
"engines": {
"node": ">= 0.10.0"
},
"author": "Your Name",
"license": "Your App's License",
"devDependencies": {
"ember-cli": "0.0.28",
"originate": "0.1.5",
"broccoli-ember-hbs-template-compiler": "^1.5.0",
"loom-generators-ember-appkit": "^1.1.1",
"express": "^4.1.1",
"body-parser": "^1.2.0",
"glob": "^3.2.9"
},
"dependencies": {
"bower": "^1.3.3",
"broccoli-template": "0.1.1",
"ember-cli": "0.0.28",
"originate": "0.1.5",
"broccoli-ember-hbs-template-compiler": "^1.5.0",
"loom-generators-ember-appkit": "^1.1.1",
"express": "^4.1.1",
"body-parser": "^1.2.0",
"glob": "^3.2.9"
}
}
Procfile:
web: npm run start
Package.json:
"scripts": {
"start": "ember serve --port=${PORT}",
"build": "ember build",
"test": "ember test",
"postinstall": "bower install"
},
and rename devDependencies block and add bower as a depency:
"devDependencies": { ... }
to
"dependencies": {
"bower": "1.3.12"
...
}
but add devDependencies again and add ember-cli there for ember to detect your app:
"devDependencies": {
"ember-cli": "0.1.15"
}
A bit more information and further links can be found from my blogpost:
https://personalwebdevelopment.wordpress.com/2015/02/23/deploying-ember-cli-app-to-heroku/
you can try with 'npm start' or 'ember server'
You could use the Ember buildpack recommended by Heroku: https://www.heroku.com/emberjs
To define this buildpack for an existing application, you'll need to run the command below:
heroku buildpacks:set https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/emberjs.tgz

Resources