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

TypeError: __require.resolve is not a function with svelte.config.cjs in an esm project #141

Closed
ai opened this issue Aug 14, 2021 · 14 comments · Fixed by #142 or #143
Closed

TypeError: __require.resolve is not a function with svelte.config.cjs in an esm project #141

ai opened this issue Aug 14, 2021 · 14 comments · Fixed by #142 or #143
Labels
bug Something isn't working

Comments

@ai
Copy link

ai commented Aug 14, 2021

Describe the bug

I have ESM project. After the update 1.0.0-next.10 → 1.0.0-next.11 I start to get error:

$ vite build
01:57:03 [vite-plugin-svelte] failed to require config /home/ai/Dev/slowreader/web/svelte.config.cjs
TypeError: __require.resolve is not a function
    at loadSvelteConfig (file:///home/ai/Dev/slowreader/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:459:42)
    at resolveOptions (file:///home/ai/Dev/slowreader/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:618:30)
    at Object.config (file:///home/ai/Dev/slowreader/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:1072:23)
    at resolveConfig (/home/ai/Dev/slowreader/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:75118:33)
    at async doBuild (/home/ai/Dev/slowreader/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:51500:20)
    at async build (/home/ai/Dev/slowreader/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:51488:16)
    at async CAC.<anonymous> (/home/ai/Dev/slowreader/node_modules/vite/dist/node/cli.js:13999:9)

My Vite config:

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'

export default defineConfig({
  plugins: [svelte()]
})

My Svelte config:

const sveltePreprocess = require('svelte-preprocess')

module.exports = {
  preprocess: sveltePreprocess()
}

Reproduction

git clone git@github.com:hplush/slowreader.git
yarn upgrade-interactive --latest
yarn build

Logs

➜ DEBUG='vite:*' npx vite build
  vite:config native esm config loaded in 53ms URL {
  href: 'file:///home/ai/Dev/slowreader/web/vite.config.js',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/home/ai/Dev/slowreader/web/vite.config.js',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
} +0ms
  vite:vite-plugin-svelte default options for production {
  extensions: [ '.svelte' ],
  hot: false,
  emitCss: true,
  compilerOptions: { format: 'esm', css: false, dev: false }
} +0ms
02:02:06 [vite-plugin-svelte] failed to require config /home/ai/Dev/slowreader/web/svelte.config.cjs
TypeError: __require.resolve is not a function
    at loadSvelteConfig (file:///home/ai/Dev/slowreader/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:459:42)
    at resolveOptions (file:///home/ai/Dev/slowreader/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:618:30)
    at Object.config (file:///home/ai/Dev/slowreader/node_modules/@sveltejs/vite-plugin-svelte/dist/index.js:1072:23)
    at resolveConfig (/home/ai/Dev/slowreader/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:75118:33)
    at async doBuild (/home/ai/Dev/slowreader/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:51500:20)
    at async build (/home/ai/Dev/slowreader/node_modules/vite/dist/node/chunks/dep-c1a9de64.js:51488:16)
    at async CAC.<anonymous> (/home/ai/Dev/slowreader/node_modules/vite/dist/node/cli.js:13999:9)


### System Info

```shell
System:
    OS: Linux 5.13 Fedora 34 (Workstation Edition) 34 (Workstation Edition)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 1.31 GB / 15.33 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.6.2 - ~/.asdf/installs/nodejs/16.6.2/bin/node
    Yarn: 1.22.10 - ~/.asdf/shims/yarn
    npm: 7.20.3 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 92.0.4515.131
    Firefox: 90.0.2


### Severity

blocking all usage of vite-plugin-svelte
@ai ai added bug Something isn't working triage Awaiting triage by a project member labels Aug 14, 2021
@ai ai changed the title TypeError: __require.resolve is not a function 1.0.0-next.10 → 1.0.0-next.11: TypeError: __require.resolve is not a function Aug 14, 2021
@bluwy
Copy link
Member

bluwy commented Aug 14, 2021

Looks like the error came from here:

delete require.cache[require.resolve(configFile)];

Apparently, esbuild compiles this to __require.resolve and __require is this 😦

var __require = (x) => {
  if (typeof require !== "undefined")
    return require(x);
  throw new Error('Dynamic require of "' + x + '" is not supported');
};

Note, it's from the esm bundle, and esbuild doesn't assign .resolve to that function too, which sort-off make sense because you can't do that in esm.

A solution is to load the cjs variant of the svelte config too if we're in esm mode so we'd never reach that part of the code in esm. (the cjs bundle compiles just fine)


As a workaround for now, you can convert the svelte config to .js and refactor it as esm, since the linked project is using type: module.

@ai
Copy link
Author

ai commented Aug 14, 2021

Great thanks! Workaround helped.

@dominikg
Copy link
Member

thanks for tackling this @bluwy

I do recommend using esm for svelte.config.js if your surrounding project is esm too. But the linked PR is going to make it work anyways.

@dominikg dominikg removed the triage Awaiting triage by a project member label Aug 16, 2021
@dominikg dominikg changed the title 1.0.0-next.10 → 1.0.0-next.11: TypeError: __require.resolve is not a function TypeError: __require.resolve is not a function with svelte.config.cjs in an esm project Aug 16, 2021
@dominikg
Copy link
Member

updated the title to be a bit more descriptive.

@bhojani3192
Copy link

I have same error

@bhojani3192
Copy link

[vite-plugin-svelte] failed to require config E:\Jar App\jar\jar\svelte.config.js

@dominikg
Copy link
Member

@bhojani3192 please file a new issue report and provide a reproduction

@bhojani3192

This comment was marked as off-topic.

@bhojani3192

This comment was marked as off-topic.

@bhojani3192

This comment was marked as off-topic.

@bluwy
Copy link
Member

bluwy commented Jul 15, 2022

please file a new issue report and provide a reproduction

@bhojani3192 If you're not willing to do so, please ask for support in the Svelte discord.

@bhojani3192

This comment was marked as off-topic.

@bhojani3192

This comment was marked as off-topic.

@bhojani3192

This comment was marked as off-topic.

@sveltejs sveltejs locked as off-topic and limited conversation to collaborators Jul 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
4 participants