You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法导入vanillajs-datepicker node模块,浏览器报404及MIME类型错误

问题描述

我计划使用JS、HTML、CSS实现一个项目,需要用到日期选择器,因此决定采用vanillajs-datepicker。

我的webpack.config.js配置如下:

const path = require('path');

module.exports = {
    mode: 'development',
    entry: './resource/js/app.js',
    output: {
        path: path.resolve(__dirname, 'public'),
        filename: 'bundle.js',
    },
};

我在app.js中编写了导入语句:import Datepicker from './../../node_modules/vanillajs-datepicker/js/Datepicker';

浏览器控制台输出如下错误:

GET http://localhost:8080/node_modules/vanillajs-datepicker/js/Datepicker
[HTTP/1.1 404 Not Found 2ms]

Loading failed for the module with source "http://localhost:8080/node_modules/vanillajs-datepicker/js/Datepicker". localhost:8080:31:1
Loading module from "http://localhost:8080/node_modules/vanillajs-datepicker/js/Datepicker" was blocked because of a disallowed MIME type ("text/html").

补充信息

我的app.js完整代码:

import Datepicker from './../../node_modules/vanillajs-datepicker/js/Datepicker';

const elem = document.querySelector('input[name="dp"]');
const datepicker = new Datepicker(elem, {
    autohide: true,
    buttonClass: "text-gray-500",
    title: 'db'
});

我的index.html代码:

<!doctype html>
<html>

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link href="css/style.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.1.4/dist/css/datepicker.min.css">
</head>

<body>
    <h1>title</h1>
    <input type="text" name="dp" class="w-5 h-5 text-gray-500 w-full">

    <script src="js/app.js" lang="text/javascript" type="module"></script>
    <script src="bundle.js" lang="text/javascript" type="module"></script>
</body>

请问是什么原因导致的错误?

解决方案

问题出在操作环节。根源是HTML中同时引用了未编译的源码和打包后的文件:

<script src="js/app.js" lang="text/javascript" type="module"></script>
    <script src="bundle.js" lang="text/javascript" type="module"></script>
</body>

删除app.js的引用后问题就解决了。


内容的提问来源于stack exchange,提问作者Maik Lowrey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:24:04