liuweiGL / vite-plugin-demand-import

Demand import for the library that has side effects
MIT License
8 stars 0 forks source link

ERR_MODULE_NOT_FOUND, 手动添加了.js 后缀就可以了 #4

Closed xinde closed 1 year ago

xinde commented 1 year ago

不知道是不是我这项目配置的问题,最新的 vite 生成ts项目,采用本插件导入 antdv3。 run dev 后报错,手动添加了.js 后缀就可以了

failed to load config from path--\vite.config.ts
error when starting dev server:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'path--\node_modules\lodash\camelCase' imported from path--\node_modules\vite-plugin-demand-import\dist\index.mjs
Did you mean to import lodash/camelCase.js?
    at new NodeError (node:internal/errors:371:5)

改了 index.mjs

import camelCase from "lodash/camelCase.js";
import kebabCase from "lodash/kebabCase.js";
import upperFirst from "lodash/upperFirst.js";

新项目中 package.json type=module 会要求引入的js文件带后缀😅 ,不是本插件的问题,关闭了

odex21 commented 1 month ago

我觉得这还得是这个插件的问题啊 就是因为改了 esm, 没办法用 vite plugin style import 才过来的 现在又是是因为 node 的 esm 规范没办法执行

import camelCase from 'lodash-es/camelCase'
console.log(camelCase('abc-123edf'))

不加 .js 后缀,在 node 20 esm 的标准就是错误的写法

现在解决办法简单的解决方法是,要么换 bun 跑 vite, 要么复制下插件代码,直接在本地跑ts