Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to resolve [plugin:vite:css] [sass] Can't find stylesheet to import. error in Vite React?

I keep getting this error: [plugin:vite:css] [sass] Can’t find stylesheet to import. in my Vite React app.

I have an App.scss, and in it, I have:

@use "~@somename/some-name/dist/_tokens.scss";

I want to reference them from the node modules, but I keep getting this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: Can't find stylesheet to import.
    ╷
  1 │ @use "~@somename/some-name/dist/_tokens.scss";
    │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ╵
    src\App.scss 1:1  root stylesheet

Inside my vite.config file, I have:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
})

>Solution :

The tilde ~ prefix used to be a convention in Webpack for referencing packages in node_modules, but Vite doesn’t support that out of the box.

Import your file like this:
@use "@somename/some-name/dist/_tokens.scss";

(Make sure you have installed the Vite’s Sass dependency:
npm install sass --save-dev)

Moreover, you might have to specify aliasing in the Vite config file:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig({
    plugins: [react()],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
        },
    },
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading