Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp-sass 5.0 how to use compiler with import() instead of require()?

Gulp-sass recently updated to version 5.0. They describe that it doesn't include a compiler anymore and they say you have to install in separately.

They have documentation on how to let gulp-sass require the compiler with this piece of code.

var sass = require('gulp-sass')(require('sass'));

But im using import() instead of require() and i can't find out how to translate the code they provided with require() to import().

This is also the error i get:

Error in plugin "gulp-sass"
Message:

gulp-sass 5 does not have a default Sass compiler; please set one yourself. 
Both the `sass` and `node-sass` packages are permitted.
For example, in your gulpfile:

  var sass = require('gulp-sass')(require('sass'));

For now i rolled back to version 4.1.1 which included the compiler but i would like to update it to version 5.0 at some point.

like image 440
Murrt Avatar asked Jun 29 '21 09:06

Murrt


2 Answers

import gulpSass from "gulp-sass";
import nodeSass from "node-sass";
    
const sass = gulpSass(nodeSass);

Try this. But I still don't know if this is the best.

  • I know "node-sass" should be deprecated. Just an example. use "dart-sass" or "sass"
like image 182
NaBeomki Avatar answered Sep 19 '22 18:09

NaBeomki


You cannot use import statement within the body of your code. It must be used at the beginning of the file (see https://flexiple.com/javascript-require-vs-import/#:~:text=One%20of%20the%20major%20differences,js%20extension%20as%20opposed%20to%20.)

If you or anyone is looking for how to implement the gulp code. I found a direction on Reddit https://www.reddit.com/r/webdev/comments/o9okup/error_with_gulpsass_setup/ a user - "LessRain" provided an answer:

To install node-sass navigate to your project directory in terminal and run this command: npm install gulp gulp-sass node-sass gulp-concat --save-dev

Then you can swap sass = require('gulp-sass'); for const sass = require("gulp-sass")(require("node-sass"));

In my case that code went into a gulpfile.js file but since it is part of a statement I had no need for "const"

let gulp = require('gulp'),sass = require("gulp-sass")(require("node-sass")),...
like image 37
Ojchris Avatar answered Sep 16 '22 18:09

Ojchris