如何使用Flask服务NPM软件包? [英] How can I serve NPM packages using Flask?
问题描述
我有一个小型Flask应用,该应用当前从外部服务器上获取jQuery和Highlight.js.我想通过NPM建立这些本地依赖项.
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.
这是什么标准做法?我应该在与static
和templates
目录相同的目录中创建package.json
文件并将node_modules
作为单独的静态目录提供吗
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?
我正在使用pip
打包和分发我的应用程序,因此任何解决方案都必须与此兼容.
I'm packaging and distributing my app using pip
, so any solution needs to be compatible with that.
推荐答案
转到静态文件夹,然后在其中初始化npm项目.
Go to your static folder and there initialize your npm project.
cd flask_app/static
$ npm init
在安装和保存npm软件包后,您可以像这样为它们提供服务:
after installing and saving npm packages you can serve them like this:
<script src="{{ url_for('static', filename='node_modules/toastr/toastr.js')}}"></script>
这篇关于如何使用Flask服务NPM软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!