How to install man on Heroku - node.js

My app needs the package: man, but by default it's not installed on the heroku/nodejs buildpack.
So according to the documentation, heroku/heroku-buildpack-apt is the tool for the job when your app needs additional apt dependencies.
I assigned the new buildpack and added a Aptfile to the root of the project with one line:
man
Here is my full package.json
{
"name": "unix-translator",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"test": "mocha --exit"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"node-dev": "^3.1.3",
"serve-favicon": "~2.4.5",
"dateTools": "file:lib/unix-command"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^4.1.0"
}
}
Here's my Procfile:
web: node ./bin/www
This gets the dependency successfully installed because I see it when I run which man now. But it doesn't work.
I get this error when I try to use the man program:
~ $ man cat
man: error while loading shared libraries: libmandb-2.7.5.so: cannot open shared object file: No such file or directory
I did found this blog post and this other blog post which both suggest the problem related to permissions and the location of files... I SSHed into my dyno and ran: /sbin/ldconfig -v and it eventually threw this error:
/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Read-only file system
^ The command needs to be run with sudo and that's not available inside a dyno. :-(
So I'm stuck again.

Not 100% sure, but this might be worth a shot:
Replace the "prestart" in your package.json with heroku-prebuild (or heroku-postbuild), as follows:
"scripts": {
"start": "node ./bin/www",
"test": "mocha --exit",
"heroku-prebuild": "apt-get update && apt-get install man"
},
You had this in "prestart", which means it gets executed when you run npm start.
However, from your question, it looks like you are accessing a one-off Heroku dyno (e.g. with "heroku run bash"), and then trying to run "man cat" therein. So when you do that, you are not running "npm start" at all on your dyno.
By putting the "apt-get" in one of the Heroku specific build steps, it executes when your slug gets built, and hence whatever you install should be available on any dyno in your app (including one-off dynos).

Related

How do we specify within a server environment which node.js start script to run?

This may be a dumb question, but I'm stumped.
I have 3 environments: Dev(local), Staging(remote) and Production(remote).
I'm writing a relatively simply Express App.
In my package.json file, I have specified start scripts for each of the 3 environments. Here's the full file:
{
"name": "test-app",
"version": "0.0.1",
"description": "test utility app",
"private": true,
"scripts": {
"start": "cross-env NODE_ENV=development nodemon ./bin/www",
"start:staging": "cross-env NODE_ENV=staging nodemon ./bin/www",
"start:production": "cross-env NODE_ENV=production && nodemon ./bin/www"
},
"devDependencies": {
"cross-env": "^7.0.3"
},
"dependencies": {
"axios": "^0.24.0",
"config": "^3.3.6",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"morgan": "~1.9.1",
"nodemon": "^2.0.15"
}
}
Now, my question is, how do I specify which start script should be used in each remote server environment?
I understand how to start the development environment, so ignore that and focus on Staging and Production.
For example, I want the start:staging to be executed in Staging and the start:production to be executed in Production.
I may be thinking about this incorrectly, so please let me know if I should be approaching the Staging and Production environments differently.
It just seems to me the Staging and Production environments would have no idea which environment they should operate as without me assigning them as one or the other somehow.
Maybe this is done as part of the workflow? I'm using github actions for build/deployment, fyi.
Update
So, I did a little more digging. In the workflow file, there's a build step:
name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present
Would I simply add something like: npm run start:staging or npm run start:production as appropriate?
You basically want to run the scripts and don't know the commands to run, right?
So
npm run start:staging
On gh-actions
- name: run app on staging
run: npm run start:staging

Can't run node express on centos server

I am trying to run an express app that I installed using express --view pug urltracker. I also executed npm install to install all dependencies.
When I start it on my laptop localhost using DEBUG=urltracker:* && npm start
it perfectly starts on localhost:3000 and my app runs smoothly but when I install it on centos 7 server, the same gets stuck at [nodemon] starting `node ./bin/www and nothing happens afterwards.
I tried running it like this as well DEBUG=backend ./bin/www but it says -bash: ./bin/www: Permission denied OR sudo: ./bin/www: command not found
I can execute a normal file like node app.js and it works fine but when it comes to running it as server, it doesn't work.
Here's my package.json
{
"name": "urltracker",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "nodemon ./bin/www"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"morgan": "~1.9.0",
"pug": "2.0.0-beta11",
"serve-favicon": "~2.4.5",
"puppeteer": "^1.6.2",
"puppeteer-select": "^1.0.3"
},
"devDependencies": {
"eslint-plugin-mocha": "^4.11.0",
"file-system": "^2.2.2",
"jest": "^21.2.1",
"js-comments": "^0.5.4",
"lodash": "^4.17.13"
}
}
I also tried it with running node supervisor using supervisor ./bin/www
but it says
Starting child process with 'node ./bin/www'
Watching directory '/home/admin/web/example.com/node/projects' for changes.
Press rs for restarting the process.
I also tried changing the port to 3001 in ./bin/www like this - server.listen('3001', '0.0.0.0');
but nothing works. Help.
Nodemon is not listed in your dependencies list in package.json and on the centos server nodemon is not present.
Please add the nodemon dependency in the package.json and then run npm i
or change the start script from "start": "nodemon ./bin/www" to "start": "node ./bin/www"

Node.js - Command npm run dev is stuck on message "Compilation complete. Watching for file changes"

When I launch npm run dev on my project, the process is stuck on the message below:
Compilation complete. Watching for file changes
and the server is not started.
I have the problem only on my Windows. When I launch the same command on my Ubuntu virtual machine, the server start as expected.
Do you know what how could I investigate more, have some logs or indications about the "error" ?
Thank you very much.
can you post your package.json file?
it is important, your script part is definded like that:
{
"name": "snail",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "nodemon app.js",
"dev":"node app.js"
},
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
}
}
app.js is the filename of yout startfile.

Node.js app doesn't work when I try to host it via GCP deploy command. Error: Cannot find module 'express'

I have my NodeJS app wrote using TypeScript and based on the Express framework. I want to host it in GCP cloud with gcloud app deploy command.
So, first of all, I build my TS sources to JavaScript -is that the correct way of doing it?.
Then from the build (with JS source code) folder I'm trying to run npm start command and it works successfully and I'm also able to check it with Preview:
.
It works well. So far, so good.
Then I run gcloud app deploy from the build folder (with built to JS sources) and I didn't see any errors during deploy.
But afterward, I receive a 500 error on each request whenever I'm trying to reach the deployed app. I've taken a look into a log and I see next error:
Error: Cannot find module 'express'
What seems to be the problem?
I tried the next commands in the build folder:
npm install
npm install express --save
npm install -g express
sudo apt-get install node-express
Nothing works for me.
Here is my package.json file:
{
"name": "full-node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"dev": "node -r ts-node/register ./src/server.ts",
"debug": "ts-node --inspect ./src/server.ts",
"start": "node build/server.js",
"prod": "npm run build && npm run start"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-node": "^7.0.1",
"typescript": "^3.0.1"
},
"dependencies": {
"#types/lodash": "^4.14.116",
"body-parser": "^1.18.3",
"connect": "^3.6.6",
"cors": "^2.8.4",
"crypto": "^1.0.1",
"express": "^4.16.3",
"firebase-admin": "^6.0.0",
"lodash": "^4.17.10"
}
}
Any idea what I'm missed? Is this the correct way to deploy an app wrote with TypeScript to GCP cloud?
app.yaml:
# [START app_yaml]
runtime: nodejs8
# [END app_yaml]
since you are running gcloud app deploy from within the build folder,probably the package.json is not deployed as npm install is run first by app engine there is no way express could be missing.you can go to gcp console and under app engine view the version and then under diagnose you can view the source(the files that were actually deployed to app engine).keep in mind that this is only possible for the standard version and not the flex.I can see from your app.yaml you are using the standard.If some files are missing then go to your app root directory and in your .gcloudignore file you can ignore the files/folders you do not want to deploy.then run gcloud app deploy from within the root directory of your project
The problem was pretty simple. Seems like gcloud app deploy use npm run build & npm run start commands to start application somewhere inside. To host Node.Js wrote on TS first we need to build it to simple JS using tsc command. Then in the build folder rewrite package.json file to use correct commands. Look at my start command: "start": "node build/server.js". I was using it inside build folder as well so that's mean gcloud command was searching in /build/build/ folder. I've changed start command to "start": "node server.js" and then all works well.

Heroku - error running browserify on Node deployment

I'm trying to deploy a Node app to Heroku, but I'm having an issue successfully running browserify when the app is deployed.
When I'm running locally, I browserify my script with npm run bundle like so (from package.json):
"bundle": "./node_modules/browserify/bin/cmd.js build/main.js -o public/scripts/bundle.js
which browserifies the script in build/main.js and puts it into public/scripts/bundle.js.
For deploying to Heroku, I added
"postinstall": "npm run bundle"
However, when I deploy, I get the following error:
Error: ENOENT: no such file or directory, open 'public/scripts/bundle.js.tmp-browserify-59309133185877094263'
Well, that's correct, that file shouldn't exist... yet. When I run npm run bundle locally, I do see that file briefly pop into existence, but then it is quickly removed and I'm left with a nice updated bundle.js.
I read through Heroku's docs on this, but I'm miffed... can anyone clarify how to get through this?
For reference, here are the relevant parts of my package.json:
"scripts": {
"bundle": "./node_modules/browserify/bin/cmd.js build/main.js -o public/scripts/bundle.js",
"postinstall": "npm run bundle"
},
"dependencies": {
"body-parser": "^1.17.1",
"browserify": "^14.1.0",
"ejs": "^2.5.6",
"express": "^4.15.2",
"jquery": "^3.2.1",
"path": "^0.12.7",
"superagent": "^3.5.2"
},
"devDependencies": {},
"engines": {
"node": "6.8.1",
"npm": "4.0.5"
}
Solved! I had bundle.js included in my global gitinore configuration. Just had to take that out, good to go!

Resources