fresh icon indicating copy to clipboard operation
fresh copied to clipboard

new project with vite-plugin throws error on first request - createDefine is not a function

Open DontMash opened this issue 2 months ago • 7 comments

I just wanted to try fresh for the first time and followed the Getting Started documentation. After successfully creating & running the project, my first request always results in the server process crashing with the attached log. As this is a vite-plugin error, creating & running a project with the builder works just fine. Is this an issue on my side?

Task dev vite

  VITE v7.1.6  ready in 1480 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help
12:37:41 AM [vite] (ssr) Error when evaluating SSR module fresh:server_entry: (0 , __vite_ssr_import_0__.createDefine) is not a function
      at eval ({{absolute_path_to_project}}/fresh-project/utils.ts:5:52)
      at async ESModulesEvaluator.runInlinedModule ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
      at async SSRCompatModuleRunner.cachedRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
      at async eval ({{absolute_path_to_project}}/fresh-project/routes/_app.tsx:5:31)
      at async ESModulesEvaluator.runInlinedModule ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
      at async SSRCompatModuleRunner.cachedRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
      at async eval (fresh-route::__app:8:31)
      at async ESModulesEvaluator.runInlinedModule ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
      at async SSRCompatModuleRunner.cachedRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
      at async eval (fresh:server-snapshot:11:31)
      at async ESModulesEvaluator.runInlinedModule ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
      at async SSRCompatModuleRunner.cachedRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
      at async eval (fresh:server_entry:7:31)
      at async ESModulesEvaluator.runInlinedModule ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
      at async SSRCompatModuleRunner.directRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
      at async SSRCompatModuleRunner.cachedRequest ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
      at async SSRCompatModuleRunner.import ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:991:10)
      at async instantiateModule ({{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18600:10)
      at async https://jsr.io/@fresh/plugin-vite/1.0.4/src/plugins/dev_server.ts:72:23
error: Uncaught (in promise) TypeError: (0 , __vite_ssr_import_0__.createDefine) is not a function
    at eval ({{absolute_path_to_project}}/fresh-project/utils.ts, <anonymous>:5:52)
    at async ESModulesEvaluator.runInlinedModule (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
    at async SSRCompatModuleRunner.cachedRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
    at async eval ({{absolute_path_to_project}}/fresh-project/routes/_app.tsx, <anonymous>:5:31)
    at async ESModulesEvaluator.runInlinedModule (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
    at async SSRCompatModuleRunner.cachedRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
    at async eval (fresh-route::__app, <anonymous>:8:31)
    at async ESModulesEvaluator.runInlinedModule (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
    at async SSRCompatModuleRunner.cachedRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
    at async eval (fresh:server-snapshot, <anonymous>:11:31)
    at async ESModulesEvaluator.runInlinedModule (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
    at async SSRCompatModuleRunner.cachedRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
    at async eval (fresh:server_entry, <anonymous>:7:31)
    at async ESModulesEvaluator.runInlinedModule (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:905:3)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1112:59)
    at async SSRCompatModuleRunner.directRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18627:22)
    at async SSRCompatModuleRunner.cachedRequest (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:1030:73)
    at async SSRCompatModuleRunner.import (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/module-runner.js:991:10)
    at async instantiateModule (file://{{absolute_path_to_project}}/fresh-project/node_modules/.deno/[email protected]_1/node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:18600:10)
    at async https://jsr.io/@fresh/plugin-vite/1.0.4/src/plugins/dev_server.ts:72:23

DontMash avatar Oct 01 '25 22:10 DontMash

(0 , __vite_ssr_import_0__.createDefine) is not a function

This smells like something is not right in your code IMHO, but without seeing the whole thing it is hard to tell. It would help if you can provide a small reproduction repository, if this problem persists.

Otherwise, setting up a fresh Fresh 2 project in a fresh folder with Vite and Tailwind works fine for me:

deno run -Ar jsr:@fresh/init

Can you please try that again and, if that works, compare that code to yours which does not work?

Here are some generic troubleshooting tips:

  • Make sure you run current stable Deno, deno --version should show -> 2.5.2
  • Hard reinstall dependencies with deno install -r --allow-scripts

See also the troubleshooting guide for other common issues and solutions.

fry69 avatar Oct 02 '25 02:10 fry69

@fry69, thanks for your fast reply!

As mentioned this is a clean new fresh-project. Steps to reproduce:

  1. Create a new fresh-project (v2.1.1) - deno run -Ar jsr:@fresh/init
  2. (Optional) Run deno install --allow-scripts
  3. Run deno task dev
  4. Access http://localhost:5173

MacBook Pro M4 Pro 48GB RAM MacOS 26 - Tahoe Deno 2.5.2 (installed via homebrew)

I will try to debug vite as mentioned in the troubleshooting guide. The error you mentioned relates to the createDefine function call in the default utils.ts file, defining the shared state of the application context. Just to clarify I did not change any code. Running the initial project and sending a request crashes the process.

DontMash avatar Oct 02 '25 11:10 DontMash

@DontMash This all looks good and should absolutely work.

As a last resort I can recommend to wipe out Deno's cache and see if that helps:

deno clean

fry69 avatar Oct 02 '25 11:10 fry69

I resolved the issue!

I did the following few steps:

  1. Run deno clean
  2. Reinstall deno via homebrew
  3. Create a new project

Since I did all these steps cumulatively, I am not able to tell which one helped. Thanks again to @fry69 for the support :+1:

DontMash avatar Oct 02 '25 14:10 DontMash

@DontMash FYI and for future reference: The core problem for this bug to trigger seems to be a package.json/node_modules existing in a parent folder, see this issue: https://github.com/denoland/deno/issues/30911

This only happens with complex setups like Fresh 2 + Vite (demo app is enough). This bug does NOT get triggered with the legacy builder.

fry69 avatar Oct 03 '25 10:10 fry69

It looks like the EnvironmentPluginContainer is doing node resolution to resolve to an ancestor node_modules/fresh folder (https://www.npmjs.com/package/fresh) instead of using Deno resolution to resolve the fresh jsr package.

    at Object.readFileSync (ext:deno_fs/30_fs.js:755:21)
    at Object.readFileSync (ext:deno_node/_fs/_fs_readFile.ts:124:19)
    at loadPackageData (file:///V:/scratch/fodler1/folder2/fresh-project/node_modules/.deno/[email protected]/node_modules/vite/dist/node/chunks/dep-Bm2ujbhY.js:1875:41)
    at resolvePackageData (file:///V:/scratch/fodler1/folder2/fresh-project/node_modules/.deno/[email protected]/node_modules/vite/dist/node/chunks/dep-Bm2ujbhY.js:1840:21)
    at tryNodeResolve (file:///V:/scratch/fodler1/folder2/fresh-project/node_modules/.deno/[email protected]/node_modules/vite/dist/node/chunks/dep-Bm2ujbhY.js:11830:25)
    at ResolveIdContext.handler (file:///V:/scratch/fodler1/folder2/fresh-project/node_modules/.deno/[email protected]/node_modules/vite/dist/node/chunks/dep-Bm2ujbhY.js:11679:16)
    at EnvironmentPluginContainer.resolveId (file:///V:/scratch/fodler1/folder2/fresh-project/node_modules/.deno/[email protected]/node_modules/vite/dist/node/chunks/dep-Bm2ujbhY.js:30904:56)

dsherret avatar Oct 03 '25 19:10 dsherret

@DontMash FYI and for future reference: The core problem for this bug to trigger seems to be a package.json/node_modules existing in a parent folder, see this issue: denoland/deno#30911

This only happens with complex setups like Fresh 2 + Vite (demo app is enough). This bug does NOT get triggered with the legacy builder.

There was indeed a node_modules-folder unintentionally in the parent directory. I noticed it yesterday while cleaning up the project. Until now I thought I messed up my cwd while debugging the issue.

DontMash avatar Oct 05 '25 09:10 DontMash