javascript - 使用 npm 在 html 中导入 javascript

标签 javascript node.js

我正在尝试添加这个 github component到我的项目。 我按照说明使用 npm

npm install pikaday --save

pikaday 和 moment 模块位于 Node 模块文件夹中,但是当我运行我的 html 文件时:

<body>
<input type="text" id="datepicker" value="9 Jun 2016">
<script src="pikaday.js"></script>
<script src="moment.js"></script>
<script>
    var Pikaday = require('pikaday')
    var picker = new Pikaday({
        field: document.getElementById('datepicker'),
        format: 'D MMM YYYY',
        onSelect: function() {
            console.log(this.getMoment().format('Do MMMM YYYY'));
        }
    });
</script>
</body>

我收到以下错误:

GET http://localhost:3000/pikaday.js 
(index):14 GET http://localhost:3000/moment.js 
(index):17 Uncaught ReferenceError: Pikaday is not defined
    at (index):17

我把路径改成

<script src="/node_modules/pikaday/pikaday.js"></script>

但得到了同样的错误

最佳答案

您需要将其作为静态文件提供。如果您使用 express :

app.use('/static/pikaday/pikaday.js', express.static('./node_modules/pikaday/pikaday.js')

然后在你的 html 中:

<script src="/static/pikaday/pikaday.js"></script>

关于javascript - 使用 npm 在 html 中导入 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44317184/

相关文章:

javascript - Swiper - OpenCart - 背景全尺寸图像不起作用

node.js - 无法确定 Architect 命令 Angular 的项目或目标

mysql - 使用 mysql2 运行大型 INSERT 查询时出现异常

javascript - 使用 Cheerio 抓取许多网站

javascript - 错误无法读取未定义的属性 'updaterCacheDirName'

javascript - 其他端运行函数 Vue, JS

javascript - Angularjs Material - 同步多个选择框

javascript - 更改 jstree 单击时的默认行为

node.js - 无法从套接字 io 接收 redis 数据

javascript - 将 then 的值保存在 Promise 外部的变量中