nodejs文件无法识别的外部js库,例如datatable / jquery

问题描述 投票:0回答:1

我有一个像这样的nodejs文件:

import projectM from '@projectM/sdk-js';

var $ = require('jquery');
require('bootstrap');

async function main() {
  //Some codes here
  $("#table_content").DataTable({
    aaSorting: [],
    responsive: true,

    columnDefs: [
      {
        responsivePriority: 1,
        targets: 0
      },
      {
        responsivePriority: 2,
        targets: -1
      }
    ]
  });

}
main();

为了编译项目,我要做:

npm run build

然后,生成dist/index.js

我将其包含在HTML页面中:

<html>
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js'></script>
</head>
<body>
<script src="./dist/index.js" charset="utf-8"></script>
</body>
</html>

当我在index.js中调用Datatable函数时,它没有被重新识别,并且收到此错误消息:

TypeError: s(...).DataTable is not a function

有没有一种方法可以将datatable js文件直接包含在index.js文件(nodejs)中?

如果没有,请问有没有办法解决这个问题?

注意,我还有许多HTML页面所需的JS文件,并且不能像我对bootstrap和jquery一样从NPM安装...任何技巧,也可以直接使用外部库而不安装它们他们使用NPM吗?

谢谢

javascript node.js
1个回答
0
投票

在您的项目中运行npm install jquery(如果没有,请先运行npm init)。节点从node_modules读取库,并且当您从html导入库时,您不会将其提供给节点使用]

© www.soinside.com 2019 - 2024. All rights reserved.