Heroku: deploying a JS web app containing a python package/venv - javascript

I made a web app made using react/node/postgresql. In this app, I'm using a js package called python-bridge which lets me run python code within a js file. This is so that I can use a python package which doesn't have a good js alternative. Below is a simple example of python-bridge and python code in a js file:
'use strict';
let assert = require('assert');
let pythonBridge = require('python-bridge');
let python = pythonBridge();
python.ex`import math`;
python`math.sqrt(9)`.then(x => assert.equal(x, 3));
let list = [3, 4, 2, 1];
python`sorted(${list})`.then(x => assert.deepEqual(x, list.sort()));
python.end();
I created a venv with $ python3 -m venv root/server in order for this file to run the python package, which worked perfectly while developing and running locally on my machine using $ source server/venv/bin/activate then $ yarn dev.
I'm trying to deploy the app to Heroku, but the python code is not running. When checking the logs, I get the same error as I did when running my app locally without activating the venv. Below, 'recipe_scrapers' is the python package which imports a scraper function, and 'scraper' is the python variable that I assign to the function.
How can I deploy to Heroku so that the python code will run properly?
I have requirements.txt with all of the python dependencies and runtime.txt with python-3.10.6 in my root. I tried adding the venv command to Procfile which didn't work (and feels like bad practice).
I'm not familiar with python, so apologies if I'm misunderstanding venvs/app deployment. Deploying a python app vs js app are the same from what I can tell, so I'm not sure what to change to get the app running properly on Heroku.

I figured out the issue while poking around my Heroku settings. Turns out I needed to add python to the Buildpacks settings. So now I have both nodejs and python buildpacks, and the dependencies are installed when deploying (python app detected... installing requirements with pip).
tbh I didn't know about buildpacks before and didn't come across them while researching this issue, but I'm glad this was a simple fix in the end.

Related

A file called pty.node.js is missing after installing "node-pty" in a electron-forge project. How can I install node-pty in Linux

After installing node-pty (an external module used to create pseudo terminals using node js) in a boilerplate electron-forge project; I found it throwing an error that some core module of node-pty is importing another module which nodejs is failing to find.
After some research I discovered that entry point of node-pty is src/index.js, which imports another module called src/unixTerminal.js (this file is imported if the system is running on linux platform and my PC is running on Ubuntu 20.04) and that module tries to import build/Releases/pty.node.js (unixTerminal.js calls many functions imported from pty.node.js, so this package cannot be ommitted) but as a matter of fact build/Releases/pty.node.js is missing and completely absent in the node_modules/node-pty folder of my project where I had installed node-pty
Why does this happen? Is this any fault of myself in installing node-pty, I had installed it directly using npm i command? If a vital file of a module is missing how can it work? Please tell me how can I use node-pty on Linux and why build/Releases/pty.node.js is missing in node-pty's directory?
Since you're using Electron Forge (a crucial detail omitted from the original post), according to this issue I found by googling "node-pty electron forge" you'll need to configure the Electron packager to unpack the pty.node file:
asar: {
unpack: '**/node_modules/node-pty/build/Release/*'
},

Deploy node.js / express / mongoDB ( with typescript ) in heroku

Before, in the backend i would not use typescript, i would just use pure javascript, but know, this is my first backend app with typescript.
I want to deploy of course this app to heroku, but, i wonder if in the procfile component i have to tell heroku to get the server running in the server.ts, or in the server.js in the dist folder.
What i'm trying to say, is that, should i compile my code to translate it into pure javascript in the dist folder in order to deplot, or is it okay just to just run the server in the server.ts file ?
This is how my project looks like
TypeScript is ultimately just there to help you structure your code properly, as it is meant to be compiled to JavaScript. While you can run your server via ts-node on a VM, it is simpler to just compile it to JS and run it on your VM, as at that point it is no different from a typical NodeJS application. Other advantages are smaller build files since your js files will take up less space and startup time.
That being said there is no stopping you from using ts-node on heroku by putting ts-node index.ts inside your package.json start script, (also ts-node should be in dependencies not devDependencies), but personally I'd go down the compilation route as that is pretty much just another NodeJS application.
See Heroku can't find ts-node

You may need an additional loader to handle the result of these loaders in NUXT app and HEROKU platform

I have a NUXT application, hosted on Heroku.
I try to deploy and suddenly I see this issue in the logs which informs me that the build failed.
I have tried to run the build script locally npm run build and it works perfectly.
Why is my code failing in the cloud but run perfectly locally?
If you don't have problems with the npm script that means that the code is fine. Taking a look back I can see that the only thing which is different is the env in which the code runs.
Most of the time the node version which you use locally is different compared with the cloud one and this might bring errors.
Find out your local node version by running node -v in the command line.
After that find out the node version in your cloud provider. In my case is Heroku and the node version is
There is clearly a difference between them.
To have the same node version running in the cloud as well, add the property engines in the package.json file and describe the node version.
This will force Heroku, to run my local version of node.js
By doing so the problem disappeared and the build was successful.

How to use NPM Modules with Flask?

I have a python application which I am deploying through Flask using the render_template() function onto a webpage. At the same time, I am trying to use npm to incorporate some javascript modules into my code. Whilst I have correctly installed the needed modules within the static folder with all my other javascript files, the code refuses to recognize these modules.
My Flask CLI shows that my local development server has correctly located the module file but if I run var module = require('module') the code shows no indication of having worked if run through the browser. This goes for whether I include this script inside my html template in the template folder, or an external javascript file in the static folder.
Interestingly enough, if I run the same external javascript file through the npm CLI using node script.js, the script will execute. Can someone explain what I'm doing wrong and why this is so? I'm completely new to node.js, npm and have just started today so any help would be appreciated.
I am currently basing my work off of the answer with 6 upvotes here: How can I serve NPM packages using Flask?
You can use electron as the ui for the python app by spawning your file and navigating to the local url in the app instead of using a browser. With this you will have some node capability.
Without knowing more, this is a bit of a stab in the dark, and quite late, however, I solved a similar problem with the following:
app = Flask(__name__,
static_folder = './public',
template_folder="./static")
npm is. Node.js packages manager tool. And it is only used node.js application.
If your application frontend is react or vue framework and your backend is node.js framework example Express or Koa, use npm is good. but now your backend is Flask
, you know Python package manage tools is Pip, so if you use Npm, you should use node in frontend , backend is flask, and frontend start npm start, backend start python app.py.

Environment variable in Javascript (Gulp)

I have a local development machine and a test server.
Now I have an APP_ID that's being used in Javascript. I've been looking into ways that they kan differ on my local machine and on the test server.
Using Gulp
With gulp it's possible to add a flag on the command line:
gulp build --env=production
That way I can get the correct APP_ID from a file.
The only issue is with this approach I need to run my build on the server, at this moment I run gulp locally and upload the changes to my server
Is it okay to build on the server? Are there other ways to use environment variables in Javascript?
My suggestion is to not build on the server but build locally and then deploy to the server using one of the many deploy solution (es. deployer.org for php). Normally javascript NPM packages put build output even in GIT repository ready for use in other projects or for deploy.
For more info on how to use env variable in node (gulp run over node) see this page
For example in linux you can set env variable with export
app.js :
console.log(console.log(process.env.foo))
Then try
> export foo=app1
> node app.js
Res:
app1
Then try
> export foo=app2
> node app.js
Res:
app2
This is valid only if you run your code server side on node (ex on gulp).
If you are developing a client side library and you want to create different builds that targets different enviroments you have to instruct gulp to do so. In this case this is a guide that can help you

Categories