[Bug]: Can not build NextJs because the error - Type error: Cannot find name 'Deno'
Problem description
Follow the demo guide and deploy guide, I want to try to do. Then I want to create an api to get Deno version which use Deno.version.deno. It works with dev mode. Unfortunately, get error Type error: Cannot find name 'Deno'
Steps to reproduce
Step 1: Follow the guide https://docs.deno.com/examples/next_tutorial/
Step 2: So I create an Nextjs api at the file app/api/hello/route.rs:
export async function GET (req: Request) {
return new Response(`Hello, from Deno v${Deno.version.deno}!`);
};
Step3: Try to build and become a failure with deno task build. It tells that:
$ deno task build
Creating an optimized production build ...
✓ Compiled successfully
Linting and checking validity of types ..Failed to compile.
./app/api/hello/route.ts:2:43
Type error: Cannot find name 'Deno'.
1 | export async function GET (req: Request) {
> 2 | return new Response(`Hello, from Deno v${Deno.version.deno}!`);
| ^
3 | };
Next.js build worker exited with code: 1 and signal: null
Expected behavior
I expect there is no error like that.
Environment
- Deno 2.2.3 (stable, release, aarch64-apple-darwin) v8 13.4.114.11-rusty typescript 5.7.3
- Mac Sequoia
Possible solution
No response
Additional context
No response
I believe you are getting this because your linter from the build step doesn't recognize the Deno object so it is flagging a type error. Try making a deno.d.ts file in your source folder. Then fill it with they key value pairs and types you are using from the deno object. Your contents would look something like this.
declare const Deno: { version: { deno: string }, };
This creates a global type and allows you to use the Deno object anywhere in your codebase.
you can run this command:
deno types > lib.deno.d.ts