Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I serve NPM packages using Flask?

I have a small Flask app which currently sources jQuery and highlight.js from external servers. I'd like to make these local dependencies which I pull in via NPM.

What is the standard practice for this? Should I create package.json file in the same directory as my static and templates directories and serve node_modules as a separate static dir ala this question?

I'm packaging and distributing my app using pip, so any solution needs to be compatible with that.

like image 577
danvk Avatar asked Jul 01 '14 16:07

danvk


People also ask

How do I connect NPM packages?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.

Can I use node js with flask?

js and Flask is useful. The most common use is when we need to add Machine Learning to our website since it is easier to implement Machine Learning in Python (due to super helpful libraries) and connect it to Node. js using Flask, rather than implementing it all together in Node. js.


1 Answers

Go to your static folder and there initialize your npm project.

cd flask_app/static $ npm init 

after installing and saving npm packages you can serve them like this:

<script src="{{ url_for('static', filename='node_modules/toastr/toastr.js')}}"></script> 

credits to: https://codeburst.io/creating-a-full-stack-web-application-with-python-npm-webpack-and-react-8925800503d9

like image 55
a2k Avatar answered Oct 02 '22 18:10

a2k