5

I have a create-react-app project, using absolute imports and css modules (scss). I am trying to define a custom font-face but the import is not being resolved.

Tried everything suggested in the documentation.

my folder structure

jsconfig.json
public
src
L assets
 L fonts
  L impact.ttf
L index.js
L index.scss

in my index.scss

@font-face {
  font-family: 'impact';
  src: url(./assets/fonts/impact.ttf) format('ttf');
}

my jsconfig.json (for absolute importing)

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

my index.js imports import './index.scss' at the top of the file. when i try to compile this code, my component throws an error saying the font file cannot be resolved...

Module not found: Can't resolve './assets/fonts/brush.ttf' in '/Users/xxx/project/src/containers/mycomponent'

Also using node-sass so the scss files work. Cant figure this one out...

2 Answers 2

5

In create-react-app the default folder for assets (Images, Fonts, and Files...) is the public folder.

So instead of creating a ./src/assets folder, just move your 'fonts' folder inside the '/public' (./public/fonts)

For more info, check: https://create-react-app.dev/docs/adding-images-fonts-and-files/

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like they are recommending the opposite now: create-react-app.dev/docs/using-the-public-folder
1

I had the same issue,

For scss in react, put the font files in the same folder as your component and point to them with relative path. Or you can put them anywhere in the src folder and refer to them.

eg

src: url(./fonts/impact.ttf)

Then webpack will collect those files and automatically pack them to later make them available at /static/fonts/impact.ttf on your server, just like your public files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.