question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when importing CJS plugin into vite.config.js when project is ESM.

See original GitHub issue

Describe the bug

Note: This is similar to https://github.com/vitejs/vite/issues/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 did import * as checker from ... rather than import checker from .... See logs below, and debugger screenshot: image

As an attempted workaround, using import * as checker and then consuming the plugin as checker.default does not work either.

Reproduction

package.json

{
  "name": "vite-cjs-default-bug",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "vite build --mode prod"
  },
  "dependencies": {
    "vue": "2.6.12"
  },
  "devDependencies": {
    "typescript": "4.4.4",
    "vite": "2.6.14",
    "vite-plugin-checker": "0.3.4",
    "vite-plugin-vue2": "1.9.0",
    "vue-template-compiler": "2.6.12"
  }
}

vite.config.js

import { defineConfig } from "vite";
import { createVuePlugin } from "vite-plugin-vue2";
import checker from "vite-plugin-checker";

export default defineConfig(async ({ command, mode }) => {
  return {
    plugins: [
      createVuePlugin(),
      checker({ typescript: true }),
    ],
  };
});

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 12.62 GB / 31.94 GB
  Binaries:
    Node: 14.18.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.15.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 95.0.4638.69
    Edge: Spartan (44.19041.1266.0), Chromium (95.0.1020.53)
    Internet Explorer: 11.0.19041.1202
  npmPackages:
    vite: 2.6.14 => 2.6.14

Used Package Manager

npm

Logs

PS C:\src\vite-no-import-cjs-default> npx vite build --debug
  vite:config native esm config loaded in 268.36ms URL {
  href: 'file:///C:/src/vite-no-import-cjs-default/vite.config.js',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/C:/src/vite-no-import-cjs-default/vite.config.js',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
} +0ms
failed to load config from C:\src\vite-no-import-cjs-default\vite.config.js
error during build:
TypeError: checker is not a function
    at file:///C:/src/vite-no-import-cjs-default/vite.config.js?t=1637015450618:9:7
    at loadConfigFromFile (C:\src\vite-no-import-cjs-default\node_modules\vite\dist\node\chunks\dep-e0fe87f8.js:68644:15)
    at async resolveConfig (C:\src\vite-no-import-cjs-default\node_modules\vite\dist\node\chunks\dep-e0fe87f8.js:68188:28)
    at async doBuild (C:\src\vite-no-import-cjs-default\node_modules\vite\dist\node\chunks\dep-e0fe87f8.js:42986:20)
    at async build (C:\src\vite-no-import-cjs-default\node_modules\vite\dist\node\chunks\dep-e0fe87f8.js:42974:16)
    at async CAC.<anonymous> (C:\src\vite-no-import-cjs-default\node_modules\vite\dist\node\cli.js:737:9)

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bluwycommented, Nov 22, 2021

This is often an issue with how the plugin is bundled. They’re exporting the Vite plugin in default with a __esModule hint that are recognized by bundlers to emulate default import. However node’s resolution doesn’t do this hence the awkward syntax. I’d recommend opening an issue in the plugin repo instead unless Vite decides to always bundle the config file, like #5779.

0reactions
bluwycommented, Feb 20, 2022

Closing as this is an issue in userland. We should fix the plugin instead so it’s compatible with node’s spec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Vite
When running vite from the command line, Vite will automatically try to resolve a config file named vite.config.js inside project root.
Read more >
vite exports is not defined | The AI Search Engine You Control
Describe the bug. error: exports is not defined when using ViteDevServer.ssrLoadModule with the following setups: package.json.
Read more >
Yarn 3.1, Vite 2.9, cannot find package vite - Stack Overflow
js Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'vite' imported from /Users/me/project/vite.config.js Did you mean to import vite-virtual- ...
Read more >
rollup.js
mjs and sits in the root directory of your project. Unless the --configPlugin or --bundleConfigAsCjs options are used, Rollup will directly use Node...
Read more >
Using vite-plugin-ssr with mdx-js, solving ESM only library ...
Please visit the website to learn how to use this plugin. In this tutorial we'll learn how to setup mdx-js library for vite...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found