Next.js Discord

Discord Forum

How to use SCSS in NextJS

Answered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
I am new to NextJS and HTML designer has created HTML using SCSS and I have put that SCSS inside public folder and I am trying to use in my layout something like this

import "./../../public/scss/_bootstrap.scss"; the location is correct .. but it keeps throwing error saying ..


./public/scss/_bootstrap.scss
To use Next.js' built-in Sass support, you first need to install `sass`.
Run `npm i sass` or `yarn add sass` inside your workspace.

Learn more: https://nextjs.org/docs/messages/install-sass


I have already installed this .. this is how my package.json file looks like...


{
  "name": "nextjs",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "bootstrap": "^5.3.3",
    "css-loader": "^6.10.0",
    "less-loader": "^12.2.0",
    "next": "14.1.4",
    "node-sass": "^9.0.0",
    "react": "^18",
    "react-dom": "^18",
    "react-scripts": "^5.0.1",
    "style-loader": "^3.3.4"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "autoprefixer": "^10.0.1",
    "eslint": "^8",
    "eslint-config-next": "14.1.4",
    "postcss": "^8",
    "sass": "^1.74.1",
    "sass-loader": "^14.1.1",
    "tailwindcss": "^3.3.0",
    "typescript": "^5"
  }
}


My layout.tsx file looks like below


import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./../../public/scss/_bootstrap.scss";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
  title: "Dashboard Page",
  description: "Dashboard page Description",
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={inter.className}>{children}</body>
    </html>
  );
}



Can someone guide me how can I resolve this ?
Answered by Ray
npm install -g sass
View full answer

123 Replies

@Nile Crocodile I am new to NextJS and HTML designer has created HTML using SCSS and I have put that SCSS inside `public` folder and I am trying to use in my layout something like this `import "./../../public/scss/_bootstrap.scss";` the location is correct .. but it keeps throwing error saying .. ./public/scss/_bootstrap.scss To use Next.js' built-in Sass support, you first need to install `sass`. Run `npm i sass` or `yarn add sass` inside your workspace. Learn more: https://nextjs.org/docs/messages/install-sass I have already installed this .. this is how my `package.json` file looks like... { "name": "nextjs", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "bootstrap": "^5.3.3", "css-loader": "^6.10.0", "less-loader": "^12.2.0", "next": "14.1.4", "node-sass": "^9.0.0", "react": "^18", "react-dom": "^18", "react-scripts": "^5.0.1", "style-loader": "^3.3.4" }, "devDependencies": { "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", "autoprefixer": "^10.0.1", "eslint": "^8", "eslint-config-next": "14.1.4", "postcss": "^8", "sass": "^1.74.1", "sass-loader": "^14.1.1", "tailwindcss": "^3.3.0", "typescript": "^5" } } My `layout.tsx` file looks like below import type { Metadata } from "next"; import { Inter } from "next/font/google"; import "./../../public/scss/_bootstrap.scss"; const inter = Inter({ subsets: ["latin"] }); export const metadata: Metadata = { title: "Dashboard Page", description: "Dashboard page Description", }; export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { return ( <html lang="en"> <body className={inter.className}>{children}</body> </html> ); } Can someone guide me how can I resolve this ?
have you tried restarting the dev server?
how does your next.config.js look like?
Nile CrocodileOP
yes i have tried to restart with no luck ..

This is how my next.config.mjs looks like


 /** @type {import('next').NextConfig} */
const nextConfig = { reactStrictMode: false };

export default nextConfig;
Please remember .. i have tailwind installed as well ..
but for other pages i want to use my custom scsss
And this is how my tsconfig.json looking like

{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": ".",
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
@Ray
Nile CrocodileOP
but i have scss folder as well which is also in public folder
and why do i need to add it beside layout.tsx ?
Nile CrocodileOP
ok
let me try
and remove the import for import "./../../public/scss/_bootstrap.scss";
Nile CrocodileOP
./app/register/_bootstrap.scss
To use Next.js' built-in Sass support, you first need to install sass.
Run npm i sass or yarn add sass inside your workspace.

Learn more: https://nextjs.org/docs/messages/install-sass
could you open up terminal
where do you run the dev server?
I think you need to start the dev server within the nextjs folder
Nile CrocodileOP
from my command prompt
@Ray
Original message was deleted
can you run ls
Original message was deleted
try rm -rf node_modules and reinstall the dependence
Nile CrocodileOP
ok
got some warnings .. but installed everything
restarted the server
still same error
ah
no idea why it doesn't work
Nile CrocodileOP
do u believe i have any issue in my code ?
or any config file i forgot anything ?
any package i missed ?
any dependency ?
could you create a new project with npx create-next-app@latest and intsall sass?
Nile CrocodileOP
these scss files designer has given me
why to create new project ?
cant we achieve same here ?
@Nile Crocodile why to create new project ?
because you have multiple loader installed on your package.json
and react-scripts
Nile CrocodileOP
please tell me which once to remove ?
i was trying to resolve this error and hence i installed multiple
"dependencies": {
    "react": "^18",
    "react-dom": "^18",
    "next": "14.1.4"
  },
  "devDependencies": {
    "typescript": "^5",
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "autoprefixer": "^10.0.1",
    "postcss": "^8",
    "tailwindcss": "^3.3.0",
    "eslint": "^8",
    "eslint-config-next": "14.1.4"
  }

these are the dependencies from a new project
Nile CrocodileOP
ok let me copy this to my json .. and let me remove node modules and reinstall
  "dependencies": {
    "next": "14.1.4",
    "react": "^18",
    "react-dom": "^18"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "autoprefixer": "^10.0.1",
    "eslint": "^8",
    "eslint-config-next": "14.1.4",
    "postcss": "^8",
    "sass": "^1.74.1",
    "tailwindcss": "^3.3.0",
    "typescript": "^5"
  }

this is after sass installed
Nile CrocodileOP
To use Next.js' built-in Sass support, you first need to install `sass`.
Run `npm i sass` or `yarn add sass` inside your workspace.

Learn more: https://nextjs.org/docs/messages/install-sass
 ⨯ ./app/register/_bootstrap.scss
To use Next.js' built-in Sass support, you first need to install `sass`.
Run `npm i sass` or `yarn add sass` inside your workspace.

Learn more: https://nextjs.org/docs/messages/install-sass
 âš  Fast Refresh had to perform a full reload due to a runtime error.
 ✓ Compiled /favicon.ico in 323ms (312 modules)
<w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: ENOENT: no such file or directory, rename '/var/www/html/nextjs/.next/cache/webpack/client-development-fallback/0.pack.gz_' -> '/var/www/html/nextjs/.next/cache/webpack/client-development-fallback/0.pack.gz'
what to do now ? 😦 m so new to NEXTJS
Nile CrocodileOP
u want me to create dummy scss file in the same "register" folder and put this code inside ?
yes
Nile CrocodileOP
ok let me try
and try importing it
Nile CrocodileOP
dummy.scss
.red {
    background-color: red;
}
putting this code inside this file
yea import it
Nile CrocodileOP
./app/register/dummy.scss
To use Next.js' built-in Sass support, you first need to install `sass`.
Run `npm i sass` or `yarn add sass` inside your workspace.

Learn more: https://nextjs.org/docs/messages/install-sass
restarted server with same error
hmm, I have no idea. It should work out of the box after sass is installed
do you have liveshare?
Nile CrocodileOP
yea
I don't see node_modules?
Nile CrocodileOP
its there
Nile CrocodileOP
i dont know why its not showing there
but its installed .. if i remove calling scss code .. my nextjs workingfine
Nile CrocodileOP
let me check
yup i can see
could you try create a new project in your home folder instead of /var
Nile CrocodileOP
why so ?
i do create my react projects in same dir working fine
maybe related to folder permission I don't know
I wasn't able to see the node_modules on liveshare
Nile CrocodileOP
u want me to give me full folder permission with 777 recursive ?
just try to init a new project in home folder first
Nile CrocodileOP
but its not working with scss only
project working fine without scss
so i believe no point changing nd shifting dir
well I don't know but nextjs support sass out of the box
Nile CrocodileOP
yea thats the problem statement here we need to resolve 😦
ls -alh node_modules/sass
ls -alh node_modules/next
check the folder permission
Nile CrocodileOP
ok
ls -alh node_modules/sass
total 4.9M
drwxrwxr-x   3 user user 4.0K Apr  4 11:22 .
drwxrwxr-x 324 user user  12K Apr  4 11:22 ..
-rw-rw-r--   1 user user  85K Apr  4 11:22 LICENSE
-rw-rw-r--   1 user user  810 Apr  4 11:22 package.json
-rw-rw-r--   1 user user 7.3K Apr  4 11:22 README.md
-rw-rw-r--   1 user user 4.8M Apr  4 11:22 sass.dart.js
-rw-rw-r--   1 user user  235 Apr  4 11:22 sass.default.cjs
-rw-rw-r--   1 user user 2.4K Apr  4 11:22 sass.default.js
-rwxrwxr-x   1 user user  451 Apr  4 11:22 sass.js
-rw-rw-r--   1 user user  343 Apr  4 11:22 sass.node.js
-rw-rw-r--   1 user user 5.3K Apr  4 11:22 sass.node.mjs
drwxrwxr-x   6 user user 4.0K Apr  4 11:22 types
ls -alh node_modules/next
total 244K
drwxrwxr-x  11 user user 4.0K Apr  4 11:22 .
drwxrwxr-x 324 user user  12K Apr  4 11:22 ..
-rw-rw-r--   1 user user   38 Apr  4 11:22 amp.d.ts
-rw-rw-r--   1 user user   50 Apr  4 11:22 amp.js
-rw-rw-r--   1 user user   89 Apr  4 11:22 app.d.ts
-rw-rw-r--   1 user user   46 Apr  4 11:22 app.js
-rw-rw-r--   1 user user   42 Apr  4 11:22 babel.d.ts
-rw-rw-r--   1 user user   54 Apr  4 11:22 babel.js
its long response
discord not allowing
look fine
Nile CrocodileOP
ok
@Nile Crocodile ok
try npm sass public/scss/_bootstrap.scss ./bs.css
Nile CrocodileOP
ok
npm sass public/scss/_bootstrap.scss ./bs.css
Unknown command: "sass"

To see a list of supported npm commands, run:
  npm help
npm install -g sass
Answer
Nile CrocodileOP
this one installed
now ?
try sass public/scss/_bootstrap.scss ./bs.css
Nile CrocodileOP
looks like working
i have another error now
looks like path not found
should be ../public/scss/_bootstrap.scss
Nile CrocodileOP
yea doing the same
path not found?
Nile CrocodileOP
@import "../../node_modules/bootstrap/scss/functions";
not found
do i need to install bootstrap now ?
so nextjs run now?
Nile CrocodileOP
not yet
ah ok
Nile CrocodileOP
wow
nextjs started now
but what we did ?
try sass public/scss/_bootstrap.scss ./bs.css
whats this ?
installed sass on your pc
@Ray `npm install -g sass`
with this
@Nile Crocodile try `sass public/scss/_bootstrap.scss ./bs.css`
this is trying to compile the scss to css only
but it failed because of failed to find the import file
its fine as long as you can start the server
Nile CrocodileOP
Thanks @Ray
Sorry for late reply .. had terrible internet connection
ok I will check it out.
Thanks again for your inputs.
Highly appreciated !!!