File on heroku not found - node.js

I have a single-page application which is written in NodeJS and deploy on Heroku.
I define my Procfile like:
web: PORT=$PORT webpack -p --config ./config/webpack/serverProd.js --progress && node ./dist/server.js
The reason why I do not use heroku-postbuild deploy hook is because I can not get the PORT variable which is assigned by Heroku dynamically, and this is the only way I can access PORT variable.
But when I run heroku run bash and search around in the container of Heroku...
ta da!
I can not found my server.js which it should be exist in the /app/dist/ directory.
I am pretty sure there is something I misunderstand. Can anyone point that out?
Really appreicate!

This is how I setup my nodejs\npm project on heroku
Procfile
web: npm run build && node dist/index.js
package.json
"scripts": {
"clean": "rm -rf dist && mkdir dist",
"serve": "node dist/index.js",
"start": "nodemon --inspect src/index.js --exec babel-node",
"build": "npm run clean && npm run build-server",
"build-server": "babel src -d dist",
"test": "mocha --compilers js:babel-register"
},
Try to start the app from dist/server.js instead of ./dist/server.js

Related

Cannot find module '/app/dist/bin/www.js'

So I am having this error on Heroku. I just woke up this morning and found it after adding a new feature. It says my www.js file is not found. This file is not on GitHub. I am writing typescript so I'm guessing Heroku will build my project and create a "./dist" folder which will include the /bin/www.js file. The application was working before and still works locally. The application still builds successfully. I have tried adding a Procfile and also used a preinstall script but nothing seems to work. Is it possible to know if the "./dist" folder was successfully created?
Error on Heroku:
Folder Structure:
Scripts:
"scripts": {
"test": "SET NODE_ENV=test&& mocha --exit -r ts-node/register tests/**/*.ts",
"dev": "nodemon src/bin/www.ts",
"start": "nodemon dist/bin/www.js",
"build": "tsc -p .",
"lint": "eslint . --ext .ts",
"precommit": "npm run lint&& npm test",
"preinstall": "npm run build"
}`
The Procfile just has: web: npm start
I found the problem. Apparently, the new build created another www.js file in "dist/src/bin/www.js" and deleted "dist/bin/www.js" on Heroku. I have no idea why this happened. So I changed my start script to "nodemon dist/src/bin/www.js".

Passing environment variable to pm2 is not working

I have two API in node js using babel and I have a package.json commands to use to make the application working this is the commands:
"build": "del-cli dist/ && babel src -d dist --copy-files",
"serve": "cross-env NODE_ENV=production node dist/index.js",
"start:noupdate": "cross-env NODE_ENV=development babel-node src/index.js",
"start:serve": "cross-env NODE_ENV=production node dist/index.js",
I have two domains one is https://api.website1.com.br and another is https://website2.com.br/api.
They have the same env file name but with another data for each database, that is .env.production and .env.development
When I make this "yarn build", my Linux execute this command :
"build": "del-cli dist/ && babel src -d dist --copy-files",
And this is working fine when I try to put in production mode on my real webservers, i go to the folder from the project and run this command to make the app online with PM2:
pm2 start npm -- run-script start:serve NODE_ENV=production
That will make this command work:
"cross-env NODE_ENV=production node dist/index.js"
The app runs just fine, but I have a problem he only runs one and doesn't create a new PM2 APP he just restarts what I start.
Example if I go to the folder in my https://api.website1.com.br and run this command first in this he starts, but I go to the another he doesn't start that but reload my already early app don't create a new one, what I'm doing wrong?
I manage to work this using pm2 ecosystem, that I found in this documentation from http://pm2.keymetrics.io/docs/usage/application-declaration/
I configure the default file and put a name my APP:
module.exports = {
apps : [{
name: "app",
script: "./app.js",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}
and use this command pm2 start ecosystem.config.js and now is working, I post here to know if someone has the same problem

Can you set aliases for npm scripts inside package.json?

I have a project written in Typescript
When developing Locally:
ts-node is installed as a dev-dependency, The commands are
to start: "ts-node src/index"
to init: "ts-node bin/init"
to init db: "ts-node bin/database-init"
to migrate db: "ts-node bin/database-migrate"
to add users: "ts-node bin/add-users"
When Deployed:
dev-dependencies are dropped, app is transpiled, the commands are
to start: "node src/index"
to init: "node bin/init"
to init db: "node bin/database-init"
to migrate db: "node bin/database-migrate"
to add users: "node bin/add-users"
Thus I am forced to maintain this in my package.json which will keep growing
"scripts": {
"start": "ts-node src/index",
"start:js": "node src/index",
"init": "ts-node bin/init",
"init:js": "node bin/init",
"db:init": "ts-node bin/db-init",
"db:init:js": "node bin/db-init",
"db:migrate": "ts-node bin/db-migrate",
"db:migrate:js": "node bin/db-migrate",
"add:users": "ts-node bin/add-users",
"add:users:js": "node bin/add-users"
},
I would much rather have a single command that works in both
to do this I have set the following alias in the deployment server
alias ts-node=/usr/bin/node
as such all these now works for both..
"scripts": {
"start": "ts-node src/index",
"init": "ts-node bin/init",
"db:init": "ts-node bin/db-init",
"db:migrate": "ts-node bin/db-migrate",
"add:users": "ts-node bin/add-users",
}
But this is not a great solution and prevents me from deploying it anywhere else.. I prefer to set the name space for right-node to ts-node || node inside the package.json this way it would be portable..
I know that the namespace for all dependencies gets added when running npm scripts, So this does happen behind the scenes, but is there any built in functionality to do this manually?
I think the best solution is to work with NODE_ENV but first, you need to install:
npm install if-env --save
and then in the script:
"scripts": {
"start": "if-env NODE_ENV=production ?? npm run start:prod || npm run start:dev",
"start:dev": "ts-node src/index",
"start:prod": "node src/index"
}
And on the server you need to set NODE_ENV to production
On Linux:
export NODE_ENV=production
And for production, I suggest using something like pm2 or foverer insted node to start an application

webpack --watch && http-server ./dist -p 8080 --cors -o not working

"server:dev": "webpack --watch && http-server ./dist -p 8080 --cors -o -d false"
When I run npm run server:dev, the script starts --watch but http-server is not running. If I shuffle the statements then http-server is running and webpack is not watching. I know webpack-dev-server solves the problem but I want to have one simple command that watches changes and starts server in browser when I build it.
Could anyone help me with this?
If you are looking for a cross-platform solution, take a look at the package npm-run-all:
{
"scripts": {
"server:webpack": "webpack --watch",
"server:start": "http-server ./dist -p 8080 --cors -o -d false",
"serve": "npm-run-all --parallel server:*"
}
}

Deploying simple nodejs app to Heroku - Procfile not working

I'm trying to deploy my Express server to my Heroku app, but every time I do, I get an error and it crashes.
I setup my Procfile with the following code: web: node /src/server.js
Where server.js is not in the root of the directory. However, when I try to start the server, I get an error in the logs: Error: Cannot find module '/src/server.js'
When I run the app locally, I have to run npm start in order for it to run the server correctly. Here is the relevant "scripts" portion of my package.json file:
"scripts": {
"start": "NODE_ENV=dev nodemon ./src/server.js --exec babel-node",
"prebuild": "rimraf dist",
"build": "babel ./src -d dist --presets es2015,stage-2 --ignore spec.js,node_modules",
"serve": "NODE_ENV=production node dist/server.js",
"start_babel": "babel-node ./server.js --preset=babel-preset-es2015",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint ."
}
Since the Procfile overrides the package.json script commands, why is it having trouble finding my file when I've explicitly set it?
I'm not sure about the leading slash. Does using ./src/server.js help at all?

Resources