-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when importing CJS plugin into vite.config.js when project is ESM. #5694
Comments
Tried upgrading to node v16.13.0, but the problem persisted. |
This is often an issue with how the plugin is bundled. They're exporting the Vite plugin in |
At now,there are some ways that maybe resolve this problem! import pkg from "vite-plugin-checker";
const checker = (pkg as any).default || pkg; second: //entry of vite-plugin-checker
module.exports = function checker(){
...
}
//not use as this
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = function checker(){
...
} Third: //entry of vite-plugin-checker
export default function checker(){
...
} At last you can also change the current project as commonjs----remove type = "module". That's all,I hope vite can fix it,because rollup doesn't have this problem.That means if a plugin can be used in rollup,it can't be used in vite as usual. |
Per @cx690 first solution, |
Unfortunately, these workarounds require abandoning type safety when using a |
Closing as this is an issue in userland. We should fix the plugin instead so it's compatible with node's spec. |
Describe the bug
Note: This is similar to #3024, but that issue is about imports within application code, not imports within Vite's config file. I believe this issue to be different, as it is being governed by the transforms being done in
loadConfigFromFile
.When importing a cjs plugin (E.g.
vite-plugin-checker
) with a default export into a esm project ("type": "module"
)'s vite.config.js file, the import instead looks as if I didimport * as checker from ...
rather thanimport checker from ...
. See logs below, and debugger screenshot:As an attempted workaround, using
import * as checker
and then consuming the plugin aschecker.default
does not work either.Reproduction
package.json
vite.config.js
System Info
Used Package Manager
npm
Logs
Validations
The text was updated successfully, but these errors were encountered: