Can't find entry file when deploying node app to heroku - node.js

I'm trying to deploy my first node app to heroku. I have set up a Procfile with the following code
web: node ./app/server.js
but when I deploy to heroku and check the logs I see the error Error: Cannot find module '/app/server.js'.
On local it works fine. I have the following in my package.json nested under scripts
"start": "nodemon ./app/server.js

Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development.
While using nodemon its better to maintain script commands for dev and production as follows:
"scripts": {
"start": "node ./app/server.js",
"dev": "nodemon ./app/server.js"
}
To determine how to start your app, The deployment server( Heroku) first looks for a Procfile. If no Procfile exists for a Node.js app, It will attempt to start a default web process via the start script in your package.json.
If you use nodemon in script, It'll internally try to run node server.js but in your case start file present in app/server.js. To avoid these issues it's better to use two separate script commands for dev and production. So that while running locally you can use npm dev command.

Related

Deploying Node.js app on Heroku successful on locally but shows Error: Cannot find module '/app/server' online

I'm new to Heroku. I have the following file structure for my app. It can successfully run if I run node server or heroku local web. I have the following script in package.json
"scripts": {
"start": "node server",
"server": "nodemon server"
},
Also, I add a Procfile with
web: node server.js
However, when I run Heroku open, it shows the following error. How Can I fix the problem?
You dont have a file called server.js under app, you need to rename the require stack so that the path is right...

What happens when I run an Angular application using the command npm start

I’m a newbie to web programming.I’ve followed a tutorial to create a simple web application with Angular CLI, then it is said that I can run the app locally using the command npm start.
It worked and my app was running on localhost:4200.
What npm start do to run my app?
I also wonder that what kind of server was being used to host my app, because I did not create any web server (E.g: Nodejs server, etc).
Is there any way to customize this server such as changing the port number?
P.s: I am using Angular 6
When you run your app using npm start, npm will try to find the configuration file package.json in your app folder.If it exists, the command specified in the start property of the scripts object will be run. In your case, the command is probably ng serve.
Then, it will use webpack-dev-server to start a local webserver. In angular.json, the folder path of the builder used to run the local server is specified in the builder property of the serve object.
Go to the builder folder, open the schema.json. You will see the default port which is 4200 is specified in this file.
The easiest way to change the port is to use ng serve with the port option. For example, if you want to run your app using port 5000, use ng serve --port 5000. You can either run this command directly on cmd or specify it in the start property of the scripts object.
When you are running the command npm start, the npm will run the command mentioned in packages.json file
In that file, we will be having various scripts such as start, build, test etc
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
Those scripts will have the command to trigger the angular CLI actions.
Basically npm start is an alias name of these script commands. You can change or create your own set of scripts.
As you already installed the npm, the nodejs gets installed in your machine. The angular CLI will use node server to run your application.
By default the port number is 4200, you can customize the port you wish to run.
Another option is to run ng serve command with the --port option e.g
ng serve --port 5050 (i.e for port 5050)
Alternatively, the command: ng serve --port 0, will auto assign an available port for use.
You can update the script in packages.json based on your needs.

Customizing Azure's npm deployment script

Azure expects by default a start script with the format of node path/to/js/file.
Neither of those things fit my setup. I have a 'start:production' script which is comprised of a series of commands joined by &&.
For example:
"start:production": "npm run build && serve -s -p 3000 dist && npm run --prefix server start",
"build": "node build/build.js",
How can one customize default Azure scripts? I saw you can somehow download and edit the default configuration scripts.
It looks like you are using Azure Web App which runs on Microsoft IIS. If so, you have no need to config the start script in the package.json file, instead, add a web.config to /wwwroot directory and config the entrance file of your node.js app there. Default web.config used for node.js apps could be found here.
And your app can't listen on port 3000 on Azure Web App, you should use process.env.PORT for handling the named pipe port in Azure Web App, see Listen additional port Microsoft Azure Nodejs.
If you want to execute some build scripts after deploying the node.js app you can either use Custom Deployment Script or add postinstall script into package.json like below:
"scripts": {
"build": "node build/build.js",
"postinstall": "npm run build"
}

How to run 2 different commands on deploy and restart using Node.js on Heroku?

I am writing a Node.js app and using Heroku to host it. And also I use Webpack + Babel to bundle all my server files into one and to use ES2015 syntax. But the thing is, I need to build my app before running it. So I put this command into package.json:
"scripts": {
"start": "webpack && node build/server.js"
},
This works, but the problem is, when I run heroku restart, my app runs only after rebuild. And the same story when my app crashes.
So I guess I need 2 different commands: one on deploy (webpack) and one in npm start (node build/server.js)
How can I accomplish this?
Actually it was not that hard. Just had to change my package.json to
"scripts": {
"heroku-postbuild": "webpack",
"start": "node build/server.js"
}
(according to this article: Heroku Node.js Support)

Running a Grunt build task on Heroku

I am deploying a Node.js application on Heroku. My production deployment is based on a Grunt task that minifies js and css files, placing them in a /dist folder from which they are called.
How can I add this step on the Heroku deployment?
Use a postinstall script, as outlined in Heroku's DevCenter docs for node:
https://devcenter.heroku.com/articles/node-with-grunt
eg:
"scripts": {
"postinstall": "grunt uglify",
"start": "node server.js"
}

Resources