Heroku - error running browserify on Node deployment - node.js

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!

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

Error: Cannot find module '#heroku/buildpack-registry'

I am trying to deploy a web app on Heroku. I am getting the following error
Error: Cannot find module '#heroku/buildpack-registry'
I tried adding the version of node I am using but still didn't help.
I also upgraded Heroku using npm i heroku#7.59.0 this too didn't help.
Any thoughts on this?
This is my package.json
{"name":"gssrDB",
"engines": {
"node": "14.18.0"
}
,
"scripts": {
"dev": "nodemon index.js",
"start": "node index.js"
},
"dependencies": {
"#heroku-cli/plugin-buildpacks": "^7.54.0",
"#heroku/buildpack-registry": "^1.0.1",
"async-file": "^2.0.2",
"cli-ux": "^4.9.3",
"dotenv": "^9.0.2",
"ejs": "^3.1.6",
"ejs-mate": "^3.0.0",
"express": "^4.17.1",
"got": "^8.3.2",
"heroku": "^7.59.0",
"mongodb": "^3.6.6",
"mongoose": "^5.12.9",
"nodemon": "^2.0.13",
"phoenix": "^1.6.0"
}
}
I solved it like this in terminal
$ heroku plugins:install buildpack-registry
$ heroku plugins:install buildpacks
may be it works
I faced a similar problem, and I solved it by using proper node version.
For the current heroku version, node16 works, otherwise it cause error in buildpack
Two things helped in fixing this:
update the heroku CLI
npm install -g heroku
Adding the engines in the package.json with the currently installed node and npm versions
"node": "14.18.0",
"npm": "7.6.3"
}

Deploy to Node App Azure App Service tsc not recognized as command

I am trying to deploy a nodejs app onto Azure App Service. I did the basics of deploying it, but it's failing to run. It seems it is designed to run "node run.js" commands rather than "npm run start".
I'm playing in the console, and if I try to run npm run start manually, I get a series of errors tied to build. Basically:
'tsc' is not recognized as an internal or external command
I'm wondering if there's something really obvious here about how tsc (and others) can be added to path. I have to admit, I'm not particularly well versed in using Azure or Node for that matter. Any help would be very much appreciated! Thanks!
This is the package.json file:
{
"name": "test-scraper",
"version": "0.1.1",
"description": "",
"main": "dist/main.js",
"scripts": {
"build": "tsc",
"build:dev": "tsc --watch",
"prestart": "npm run build",
"start:dev": "nodemon",
"start": "pm2 start dist/src/main.js --node-args=\"-r ./tsconfig-paths-bootstrap.js\" && pm2 monit",
"stop": "pm2 delete main"
},
"author": "",
"license": "MIT",
"devDependencies": {
"#types/lodash": "^4.14.161",
"#types/node": "^14.11.8",
"#types/puppeteer": "^3.0.2",
"nodemon": "^2.0.4",
"prettier": "^2.1.2",
"typescript": "^4.0.3"
},
"dependencies": {
"axios": "^0.20.0",
"discord-webhook-node": "^1.1.8",
"lodash": "^4.17.20",
"messaging-api-telegram": "^1.0.1",
"playwright-firefox": "^1.4.2",
"pm2": "^4.5.0",
"tsconfig-paths": "^3.9.0",
"winston": "^3.3.3"
}
}
Run this command locally for installing typescript, because your code is compiled with the tsc command.
npm install -g typescript
Mostly I was following this tutorial.
Add the tsc command to package.json and add the dependencies:
"build": "tsc --project ./"
...
"devDependencies": {
"#types/express": "^4.17.9",
"#types/node": "^14.14.20",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
},
Here is my file structure:
I add a empty file.ts and add this scripts to the tsconfig.jason file:
"exclude": [ "src", "wwwroot" ],
"include": [ "file.ts" ]
Deploy through Azure Web App deployment center:
And the app build (tsc) run successfully:
if you are deploying to Production (NODE_ENV: production), devDependencies will no longer being installed, and you need to edit your package.json and move typescript to dependencies.
Source

How to install man on Heroku

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).

How to get phantomjs to install on elastic beanstalk?

I have been google and reading different SO and github issues about this for the last few hours. I have tried adding a phantomjs config file to an .ebextensions folder, I have tried including a buildspec file that installs phantom js, and a bunch of other things and I cant seem to get phantomjs installed on my elastic beanstalk instance running nodejs. This is the package.json and the error. Any ideas as how to get this working would be really helpful. Thanks!
This is the package.json
{
"name": "webcrawler",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec 'npm test'",
"start": "node app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"#types/cheerio": "^0.22.6",
"#types/phantom": "^3.2.3",
"cheerio": "^1.0.0-rc.2",
"express": "^4.16.2",
"line-by-line": "^0.1.6",
"node-schedule": "^1.2.5",
"phantom": "^4.0.12",
"phantomjs-prebuilt": "^2.1.16",
"request": "^2.83.0",
"write": "^1.0.3"
},
"devDependencies": {
"#types/chai": "^4.0.10",
"#types/express": "^4.11.0",
"chai": "^4.1.2",
"mocha": "^4.0.1",
"sinon": "^4.1.3"
}
}
error
phantomjs-prebuilt#2.1.16 install /tmp/deployment/application/node_modules/phantomjs-prebuilt
node install.js
PhantomJS not found on PATH Download already available at /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 Verified
checksum of previously downloaded file Extracting tar contents (via
spawned process) Removing
/tmp/deployment/application/node_modules/phantomjs-prebuilt/lib/phantom
Copying extracted folder
/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1515525280131/phantomjs-2.1.1-linux-x86_64
-> /tmp/deployment/application/node_modules/phantomjs-prebuilt/lib/phantom
Running npm install:
/opt/elasticbeanstalk/node-install/node-v7.10.1-linux-x64/bin/npm
Setting npm config jobs to 1 npm config jobs set to 1 Running npm
with --production flag Failed to run npm install. Snapshot logs for
more details. UTC 2018/01/09 19:17:20 cannot find application npm
debug log at /tmp/deployment/application/npm-debug.log
Traceback (most recent call last):
File "/opt/elasticbeanstalk/containerfiles/ebnode.py", line 695, in ....
Not the most ideal solution, but I downgraded the elastic beanstalk instance to an earlier version an then it started to work.

Resources