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

(bug: ssr) NodeRequire is undefined when call ssrLoadmodule in entry-server file #5424

Closed
7 tasks done
zhangyuang opened this issue Oct 26, 2021 · 6 comments
Closed
7 tasks done
Labels
feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority) pending triage

Comments

@zhangyuang
Copy link
Contributor

zhangyuang commented Oct 26, 2021

Describe the bug

ssrModuleLoader function only inject NodeJs.Global in context but not NodeRequire which cause require is not defined error occur when evaluating !entry module. I can't determine whether vite do this on purpose.here is a pr maybe fix the problem

Reproduction

https://github.com/zhangyuang/vite-ssr-error-require

$ yarn && yarn dev &&  curl http://127.0.0.1:3000/

image

System Info

$ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers

  System:
    OS: macOS 10.15.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 1.44 GB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.17.2 - ~/.nvm/versions/node/v14.17.2/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 95.0.4638.54
    Safari: 13.1
  npmPackages:
    @vitejs/plugin-vue: ^1.0.0 => 1.9.3
    @vitejs/plugin-vue-jsx: ^1.0.0 => 1.2.0
    vite: ^2.6.11 => 2.6.11

Used Package Manager

yarn

Logs

curl http://127.0.0.1:3000/
ReferenceError: require is not defined
    at /src/entry-server.js:5:13
    at async instantiateModule (/Users/yuuang/Desktop/ssr-vue/node_modules/vite/dist/node/chunks/dep-92cbd8f1.js:66520:9)%

Validations

@zhangyuang
Copy link
Contributor Author

#5425

@zhangyuang zhangyuang changed the title NodeRequire is undefined when call ssrLoadmodule in entry-server file (bug: ssr) NodeRequire is undefined when call ssrLoadmodule in entry-server file Oct 26, 2021
@Shinigami92 Shinigami92 added feat: ssr has pr p3-minor-bug An edge case that only affects very specific usage (priority) labels Oct 26, 2021
@Shinigami92 Shinigami92 linked a pull request Oct 26, 2021 that will close this issue
4 tasks
@sodatea
Copy link
Member

sodatea commented Dec 3, 2021

As far as I know, in a Node.js ES module you need to use createRequire to safely create a require function with the right context.
So I think Vite is working as expected here.

@zhangyuang
Copy link
Contributor Author

As far as I know, in a Node.js ES module you need to use createRequire to safely create a require function with the right context. So I think Vite is working as expected here.

What do you mean is use createRequire to create require rather than inject require in new Function in vite?

@sodatea
Copy link
Member

sodatea commented Dec 3, 2021

https://nodejs.org/api/esm.html#no-require-exports-or-moduleexports

@zhangyuang
Copy link
Contributor Author

Get

@bluwy
Copy link
Member

bluwy commented Mar 28, 2022

I'm not sure if this is something we should support. I remember in a team meeting before that ssrLoadModule is used to load an ESM module, hence its function name. And there's no require() in ESM. So I don't think this is something we want. Leaving this open if others have some thoughts on this.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority) pending triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants