vite-plugin / vite-plugin-commonjs

A pure JavaScript implementation for CommonJs
MIT License
96 stars 14 forks source link

TypeError: commonjs is not a function #13

Closed shenghan97 closed 1 year ago

shenghan97 commented 1 year ago

I've been unable to use the plugin in Vite 3. Here's the error log. Thanks in advance!

failed to load config from (rootDir)/vite.config.js
error when starting dev server:
TypeError: commonjs is not a function
    at file://(rootDir)/vite.config.js.timestamp-1662774209726.mjs:6:22
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadConfigFromBundledFile (file:///(rootDir)i/node_modules/vite/dist/node/chunks/dep-665b0112.js:63419:21)
    at async loadConfigFromFile (file:///(rootDir)i/node_modules/vite/dist/node/chunks/dep-665b0112.js:63305:28)
    at async resolveConfig (file:///(rootDir)/node_modules/vite/dist/node/chunks/dep-665b0112.js:62904:28)
    at async createServer (file://(rootDir)/node_modules/vite/dist/node/chunks/dep-665b0112.js:61978:20)
    at async CAC.<anonymous> (file://(rootDir)/node_modules/vite/dist/node/cli.js:700:24)

Here's my config:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import commonjs from 'vite-plugin-commonjs'

export default defineConfig({
  plugins: [react(),commonjs()],

  clearScreen: false,
  server: {
    port: 1420,
    strictPort: true,
  },

  envPrefix: ["VITE_", "TAURI_"],
  build: {
    target: ["es2021", "chrome100", "safari13"],
    minify: !process.env.TAURI_DEBUG ? "esbuild" : false,
    sourcemap: !!process.env.TAURI_DEBUG,
    commonjsOptions: {
      include: [/src/,/node_modules/,/linked-dep/],
      transformMixedEsModules: true
    }
  },
});
caoxiemeihao commented 1 year ago

In your package.json have type: "module". You can try to use commonjs.default. type: "module" will supported in next version.

shenghan97 commented 1 year ago

Okay great! Thank you.