37

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
  • 513
  • 1
  • 5
  • 8
  • The problem shows up when using CommonJS syntax (require) in a ES module project: https://youtu.be/TXcFnsY5aqQ?t=237 – Benny Code Feb 27 '23 at 13:23

4 Answers4

18

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.

17

work for me , only change in package.json file, this line:

"type":"module" to "type":"commonjs"

0

This error occurs because require is a CommonJS module syntax, whereas import is an ES6 module syntax. to solve this, you should use import instead of require ; and in you json package the type must contain "type": "module" if you are using Node js

M.GR
  • 39
  • 4
-3
  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...
Basit Ali
  • 37
  • 1