5

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?

Leuven Wang
  • 141
  • 2
  • 12

3 Answers3

0

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.

Arnav Chawla
  • 444
  • 3
  • 15
0

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")
Tim Pozza
  • 498
  • 6
  • 9
-1

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.

labike
  • 15
  • 9
  • considering the use of a template that uses npm, node and webpack, i should I run both flask run and npm start? is there anyway to only use flask run and the static files ? – lowercase00 Apr 12 '19 at 06:29