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

fix: allow user to add Vitepress configuration to ESM packages #339

Closed
wants to merge 1 commit into from

Conversation

joaotavora
Copy link

If users have a type: module entry in their package.json, they cannot use VitePress with a config.js file.

Reproduction

  1. Follow the tutorial
    and also a "type": "module" entry to the package.json file, so that it
    becomes:
{
  "name": "vitepress-starter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "vitepress": "^0.15.6"
  }
}
  1. Then, adding a docs/.vitepress/config.js as suggested here

  2. This will result in a typical ERR_REQUIRE_ESM error:

❯ npx vitepress dev docs
vitepress v0.15.6
vite v2.3.7
failed to start server. error:
 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/capitaomorte/Source/Javascript/vitepress-starter/docs/.vitepress/config.js
require() of ES modules is not supported.
require() of /home/capitaomorte/Source/Javascript/vitepress-starter/docs/.vitepress/config.js from /home/capitaomorte/Source/Javascript/vitepress/dist/node/config.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /home/capitaomorte/Source/Javascript/vitepress-starter/docs/.vitepress/config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/capitaomorte/Source/Javascript/vitepress-starter/package.json.

    at new NodeError (node:internal/errors:278:15)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1125:13)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:997:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at resolveUserConfig (/home/capitaomorte/Source/Javascript/vitepress/dist/node/config.js:57:40)
    at async Object.resolveConfig (/home/capitaomorte/Source/Javascript/vitepress/dist/node/config.js:17:24)
    at async Object.createServer (/home/capitaomorte/Source/Javascript/vitepress/dist/node/server.js:8:20) {
  code: 'ERR_REQUIRE_ESM'

Proposed fix

This change makes it so that config.cjs is searched before config.js. Node's 'require' function allows files with such extensions to be loaded even from within an ES module project.

If users have a `type: module` entry in their `package.json`, they
could not use VitePress with a `config.js` file.

This change makes it so that `config.cjs` is searched before
`config.js`.  Node's 'require' function allows files with such
extensions to be loaded even from within an ES module project.

* docs/guide/configuration.md: Mention config.cjs

* src/node/config.ts: Consider config.cjs
@yyx990803 yyx990803 closed this in d3b1521 Oct 7, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant