question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prependData for sass-loader not working anymore

See original GitHub issue

Bug report

Describe the bug

Inside my next.config.js file I have

sassOptions: {
            includePaths: [getAbsPath('./public/sass')],
            prependData: `$staticPath: "${staticPath}"; @import "./public/sass/settings/index.scss"; @import "./public/sass/tools/index.scss";`,
        },

this used to work on next version 9.5 but recently we upgraded to 10 version and it reports undefined Sass variables on running the project with the same configuration.

prependData was released before June - https://github.com/vercel/next.js/pull/12277

But also noticed that sass-loader removed prependData property here -> https://github.com/webpack-contrib/sass-loader/releases/tag/v9.0.0

Now, next 10 is dart-sass by default.

So, how do I cope with this change in our project?

Error that I get right now is

./shared/NoInternetConnection.module.scss
SassError: Undefined variable.

System information

  • OS: macOs
  • Version of Next.js: [e.g. 10.0.3]
  • Version of Node.js: [e.g. 10.19.0]
  • Deployment: next start
  • sass version - tested on 1.29.0 and 1.26.10

Additional context

Should you need any more information, please let me know in case I missed anything. Apart from sass, there is no other dependencies installed. Assuming next.js automatically handles sass-loader configuration. Also, I tried additionalData but in vain considering the fact that sass-loader has removed prependData. I believe the support is needed for additionalData now.

I am running custom express server to render this configuration.

server.js

const next = require('next');
const express = require('express');
const nextConfig = require('./next.config');

const isDev = process.env.NODE_ENV !== 'production';
// const nextConfig = getNextConfig({}, !isDev);
const app = next({ dev: isDev, conf: nextConfig});
const handle = app.getRequestHandler();
const port = process.env.PORT || 3005;

app.prepare().then(() => {
    const server = express();
    server.all('*', (req, res) => {
      return handle(req, res);
    });
    server.listen(port, (err) => {
      if(err) throw err;
      console.log(`> Ready on localhost: ${port} - env ${process.env.NODE_ENV}`)
    });
});

NoInternetConnection.module.scss

.container {
...
    background: $white;
...
...
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
marcvangendcommented, Feb 14, 2021

For anyone who (like me) finds this issue trying to solve a “SassError: Undefined variable” problem using Next.js 10: Here’s what worked for me.

I have a ‘styles’ directory with the files _variables.scss (defining $color-rebeccapurple: #663399;) and Home.module.scss. My next.config.js looks like this:

module.exports = {
  sassOptions: {
    prependData: `@use 'variables' as *;`,
  },
};

Note that I’m using @use instead of @import because the latter will be phased out. Also note the as * part to strip off the namespace (see documentation). Now I can use color: $color-rebeccapurple in my Home.module.scss. (Alternatively you could import with @use 'variables'; so it remains namespaced, and write color: variables.$color-rebeccapurple in your scss code.)

2reactions
kushalmahajancommented, Dec 22, 2020

Plus environment variables should be available as normally to next build via .env or .env.development files.

I used dotenv-flow to make it work for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass Loader Error: Invalid options object that does not match ...
I'm getting this error after running npm run serve: Sass Loader has been initialised using an options object that does not match the...
Read more >
sass-loader - webpack - JS.ORG
This allows you to control the versions of all your dependencies, and to choose which Sass implementation to use. Note. We highly recommend...
Read more >
sass-loader/CHANGELOG.md - UNPKG
10, * added support for automatic loading of `sass-embedded` ([#1025](https://github.com/webpack-contrib/sass-loader/issues/1025)) ...
Read more >
node_modules/sass-loader/CHANGELOG.md · master - GitLab
Bare imports not working sometimes (#579) (c348281), closes #566 ... The sass-loader throws an error at runtime now and refuses to compile ...
Read more >
sass-loader - Bountysource
When using Dart Sass and not using fibers , the renderSync method should always ... (related to https://github.com/webpack-contrib/sass-loader/issues/802) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found