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.
Related
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.
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
I have a set of JS files which I am currently using for a server side node.js API.
Files:
CommonHandler.js
Lib1.js
Lib2.js
Lib3.js
I need to reuse these JS files inside an ASP.NET application.
How can I bundle these files and reuse it for other applications? One of the options I can think of is to create an NPM package and include the NPM package inside an ASP.NET application. However, I do not want to upload internal JS files to public NPM server. Can the package be uploaded to an internal Nexus server? Has anyone done similar thing before? Is there any better solution?
Thanks.
You can use pkg to to package your Node.js project into an executable that can be run even on devices without Node.js installed
I am writing a javascript library that is npm linked to my meteor app. I didn't yet publish the library to npm so it is only local.
Now my problem is, that I want to debug inside the library while using it in my meteor app.
I can debug the meteor app components and using a sourcemap and a standalone html test page also the library itself. But as soon as I use import Library from 'library' in meteor it minifies, uglifies and mangles it.
Is there a way to tell either meteor's build system or my browser to use the library's source map?
Thanks in advance
You can try removing the Meteor minifier package until you need it again:
meteor remove standard-minifier-js
I am very new to Node.js (which I'm assuming this is; I'm so new that I'm not really understanding what's going on here). I'm working with a client library for a system called RJ Metrics. I'm basically tying their API in with a Volusion API in order to import data into their system from the Volusion site. The code for that all makes sense but I'm not understanding how to install it and use it.
Their documentation is here:
And I'm needing to use the Javascript library because I'm working with Volusion which is on a Windows server and there is no ASP/C# option here. It says The RJMetrics Javascript client library is available via npm: and then terminal code. After research, it appeared that this uses Node.js so I installed that on my computer and ran the npm install rjmetrics in the Terminal which succeeded. I was assuming though that I must have to log into their server and run the code in order to get it to work.
Does this require me to SSH into the server? Am I way off base and is there a way I can just include some JS files in my page? I looked at their GitHub too and all of the main files use the require() function in them which I'm gathering is a Node.js function?
Apologies if I'm way off, I'm into this up to my neck and just trying to sort it all out now.
This part of the documentation (to which you refferred) is just plain ol' javascript. though NPM is the node package manager. So if you want, it looks like you can just run this .js script in a web browser like any other.
var rjmetrics = require("rjmetrics");
client = rjmetrics.Client(api_key, client_id);
# do stuff with client
If you wanted to do it in Node, you would create a .js file on your machine with their API code inside of it doing whatever you want. Then in terminal you run the script by going "node myfile.js". A local webserver setup is all you need to create and test this.