Skip to content
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: exports is not defined when using ViteDevServer.ssrLoadModule #2890

Closed
yaquawa opened this issue Apr 7, 2021 · 3 comments
Closed
Labels
duplicate This issue or pull request already exists pending triage

Comments

@yaquawa
Copy link
Contributor

yaquawa commented Apr 7, 2021

Describe the bug

error: exports is not defined when using ViteDevServer.ssrLoadModule with the following setups:

package.json

{
  "name": "@yaquawa/vite-plugin-ssr",
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs"
    },
    "./BootSSRServer": "./dist/BootSSRServer.cjs",
    "./BootSSRClient": "./dist/BootSSRClient.cjs"
  }
}

index.js

{
  name: 'vite-plugin-ssr',
  config: () => ({
    ssr: {
      external: [
        `@yaquawa/vite-plugin-ssr/BootSSRServer`,
        `@yaquawa/vite-plugin-ssr/BootSSRClient`
      ]
    }
  }),

  resolveId (id, importer, options, ssr) {
    if (id !== 'virtual:BootSSR') {
      return
    }

    const bootSSR = `@yaquawa/vite-plugin-ssr/${ssr
      ? 'BootSSRServer'
      : 'BootSSRClient'}`

    return bootSSR
  }
}

so basically, this won't work even if it really should.

import xxx from '@yaquawa/vite-plugin-ssr/BootSSRClient'

Dirty Workaround?

Change the the "exports" field in package.json to *.mjs and return require.resolve(bootSSR) instead of the package name itself works:

package.json

{
  "name": "@yaquawa/vite-plugin-ssr",
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs"
    },
    "./BootSSRServer": "./dist/BootSSRServer.mjs",
    "./BootSSRClient": "./dist/BootSSRClient.mjs"
  }
}

index.js

{
  name: 'vite-plugin-ssr',
  config: () => ({
    ssr: {
      external: [
        `@yaquawa/vite-plugin-ssr/BootSSRServer`,
        `@yaquawa/vite-plugin-ssr/BootSSRClient`
      ]
    }
  }),

  resolveId (id, importer, options, ssr) {
    if (id !== 'virtual:BootSSR') {
      return
    }

    const bootSSR = `@yaquawa/vite-plugin-ssr/${ssr
      ? 'BootSSRServer'
      : 'BootSSRClient'}`

    return require.resolve(bootSSR)
  }
}

Is this the intended way to resolve package name(Subpath exports) in the SSR mode..?!

System Info

vite: 2.1.5
node: v14.11.0

Used package manager: yarn

@zzzej
Copy link

zzzej commented Apr 14, 2021

I solved it temporarly by using createRequire as suggested here #2074 (comment)

This is an example:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const cookie = require('cookie');

@furudean
Copy link

Duplicate of #2579?

@muellercornelius
Copy link

Duplicate of #2579?

yep 👍🏽

@Shinigami92 Shinigami92 added the duplicate This issue or pull request already exists label Apr 16, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists pending triage
Projects
None yet
Development

No branches or pull requests

6 participants