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

20,290

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.

Share:
20,290
askcoder
Author by

askcoder

Updated on January 16, 2022

Comments

  • askcoder
    askcoder over 2 years

    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

  • askcoder
    askcoder over 2 years
    { "name": "myfirsttheme", "version": "1.0.0", "description": "description", "main": "index.js", "type": "module",
  • askcoder
    askcoder over 2 years
    you mean "type":"commonjs"?
  • Admin
    Admin over 2 years
    @askcoder If you're using require, the type should be commonjs.
  • Admin
    Admin over 2 years
    Please add further details to expand on your answer, such as working code or documentation citations.
  • askcoder
    askcoder over 2 years
    I have updated the question with new details
  • Nitneq
    Nitneq over 2 years
    Using type "commonjs" in my package.json do the trick for me ! Thanks