node_modules 找不到路径

node_modules path can't be found

我的项目结构如下所示:

在我的 index.html 中,我试图访问 node_modules 中的特定 js。

我试过这些:

<script src="./node_modules/ng-currency/dist/ng-currency.js"></script>

<script src="../node_modules/ng-currency/dist/ng-currency.js"></script>

<script src="../../node_modules/ng-currency/dist/ng-currency.js"></script>

None有效,只有将node_modules文件夹复制到public文件夹时才有效,有没有更好的方法?

您在客户端 js 文件中引用的每个 javascript 文件都不需要位于 public 文件夹中!

您可以将其导出为 app.js 文件中的静态资产,然后像您已经在做的那样在 html 中使用它。如果您使用的是 express 那么它非常简单。

app.js:

app.use('/scripts', express.static(path.join(__dirname, '/node_modules/ng-currency/dist/')));

html:

<script src="/scripts/ng-currency.js" type="text/javascript"></script>