ReferenceError: require is not defined in ES module scope, you can use import instead gulp sass

Viewed 43791

I got an error when using this line of code

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

The error output is :

Requiring external module babel-register
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'C:\xampp-clean\htdocs\myfirsttheme\package.json' contains "type": "module". T
o treat it as a CommonJS script, rename it to use the '.cjs' file extension.

I have changed

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

the error disappeared but I am getting this error :

Requiring external module babel-register
TypeError: Cannot read property 'prod' of undefined

and this line is causing the error :

const PRODUCTION = yargs.argv.prod;

Do anyone has an idea?

I have tried a lot and googled this error but I did not find a solution , do anyone has an idea about this ? Thanks in advance

2 Answers

Can you check the "type" part in the package.json file? If the type part is module, you will get this error. You have to make it Commonjs.

  1. First of all add "type":"commonjs" to package.json.
  2. Downgrade your package version. If you are using gulp-imagemin version 8.0.0 then install older version like npm i gulp-imagemin@7.1.0.
    That's all now enjoy coding...
Related