跳转到内容

关于 JavaScript 模块导入的文件扩展名问题

在 JavaScript 的 ES 模块系统中,文件扩展名的处理确实有些混乱,这主要取决于你的运行环境和配置。

为什么有时需要扩展名,有时不需要

  1. Node.js 环境

    • 在 Node.js 的 ES 模块系统中,导入本地文件时必须包含文件扩展名(.js.mjs等)
    • 这是 Node.js 的设计决定,与浏览器的 ES 模块加载行为保持一致
  2. 打包工具环境(如 Vite、Webpack):

    • 这些工具通常会自动解析扩展名,所以可以省略
    • 它们有自己的模块解析算法,会尝试添加各种扩展名

解决方案

修改你的导入语句,添加 .js 扩展名:

javascript
// javascript:zzjtnb/.vitepress/plugins/auto/test/test.js
import * as allExports from '../index.js'

console.log('🚀 ~ allExports:', allExports)
// // 使用示例
// async function main() {
//   const fileTree = await new VitePressGenerator({
//     baseDir: 'src',
//     include: ['博客'],

//     // debug: true,
//   }).initialize()
//   console.log('🚀 ~ main ~ fileTree:', fileTree)
//   // fileTree.fileTree 已经包含了构建好的文件树
// }
// main()

最佳实践

为了避免这类问题,建议:

  1. 在 Node.js 环境中,始终在导入本地文件时包含扩展名
  2. index.js 文件中导入其他模块时,也要包含扩展名:
javascript
// 在 index.js 中
export * from './utils/fileTreeBuilder.js'
export * from './utils/generator.js'
  1. 只有导入 npm 包时可以省略扩展名:
javascript
import express from 'express' // 这样是可以的

这样做可以确保你的代码在不同环境中都能正常工作。