vercel / geist-font

https://vercel.com/font
SIL Open Font License 1.1
2.37k stars 64 forks source link

SyntaxError: Cannot use import statement outside a module #13

Closed darnfish closed 11 months ago

darnfish commented 11 months ago

Font Name (Geist Sans/Geist Mono):

Description of the Issue:

project_root/node_modules/geist/dist/font.js:1
import localFont from "@next/font/local";
^^^^^^

SyntaxError: Cannot use import statement outside a module

Environment (please complete the following information):

Additional Context: My package.json does not include type: module

JohnPhamous commented 11 months ago

@darnfish can you provide a Codesandbox/Replit/repo that repros the problem?

washedlih commented 11 months ago

have this issue as well

ckrook commented 11 months ago

Hello, me to!

stijnelskens commented 11 months ago

Bump, also having this issue

ethndotsh commented 11 months ago

me as well

heyarviind commented 11 months ago

getting same issue

iamdevmarcos commented 11 months ago

the same issue here

aryanprince commented 11 months ago

Apparently this error only shows up on pages router. Can someone vouch for this?

I built two new Nextjs 14 apps (app and pages), installed Geist font, and added GeistSans.classNames to _app.tsx and layout.tsx respectively. App router version works fine, pages router one shows the same 'Cannot use import statement outside a module' error.

Link to repos I tested this with: Pages Router (error) & App Router (all good).

Feel free to StackBlitz these since I couldn't get that to work.

jacobdalamb commented 11 months ago

Also couldn't get Geist package to work in Astro

dieharders commented 11 months ago

Same issue. Using "pages" router.

When using import { GeistSans, GeistMono } from 'geist/font' I get the SyntaxError same as OP. However, when using import localFont from "next/font/local" and then manually downloading fonts into my project is the only way I can load this font without getting the error.

Next.js: v14.0.1 React: v18.2.0 Using PNPM package manager.

eriksik2 commented 11 months ago

Also getting this error on nextjs v13.5.4 with pages router.

carloscdante commented 11 months ago

Also getting this error on Next 13.4.16 w/pages router

AnimeAllstar commented 11 months ago

Me too. Looks like they didn't bother testing it with the pages router.

JonHMChan commented 11 months ago

Also getting this error with pages

benjypng commented 11 months ago

Having this issue with pages router as well.

enricoros commented 11 months ago

Same - pages router, NextJS 13.4.19 (not updating to13.5/14 because of larger bundle sizes)

spidgorny commented 11 months ago

This is a fix that worked for me (next.config.js):

transpilePackages: ["geist"],
kiltedDev commented 11 months ago

This is a fix that worked for me (next.config.js):

transpilePackages: ["geist"],

This fixed it for me as well.

"next": "^13.5.4",
"react": "18.2.0",

using Bun as package manager for this one.

darnfish commented 11 months ago

This is a fix that worked for me (next.config.js):

transpilePackages: ["geist"],

Thanks @spidgorny! This worked for me. I had to also add font-sans to my root Tailwind component for the font to actually render, Geist docs makes this kind of difficult to figure out.

VapidLinus commented 11 months ago

@darnfish I don't think this issue should be closed.

It's great we found a workaround, but this is still an issue imo until they add this to the installation instructions on the website or the npm page.

benjypng commented 10 months ago

@darnfish I don't think this issue should be closed.

It's great we found a workaround, but this is still an issue imo until they add this to the installation instructions on the website or the npm page.

I think so too. It seems like the source of the issue has not been discovered yet.

aryanprince commented 10 months ago

I made a new issue (#59) with more information about this issue, check it out. Hopefully it gets resolved.

@VapidLinus @hkgnp

troneras commented 3 months ago

Try adding this line on the top of your fonts file

/// <reference types="geist/font/sans" />

Explanation: You may have in your tsconfig a rule to exclude node_modules, which means TypeScript will not check or include any files from the node_modules directory.

One solution could be to remove "node_modules" from the exclude array, but this might significantly slow down TypeScript, as it will start checking all files in node_modules.

A better solution is to add that triple slash directive on the top of the file. This directive will tell TypeScript to include that specific module in the compilation context. This way, TypeScript will include the geist/font module in the compilation context, even though node_modules is excluded in tsconfig.json.

If it doesn't work try restarting the ts server: