首页 > 解决方案 > ReferenceError: require 没有在 ES 模块范围内定义,你可以使用 import 代替 gulp sass

问题描述

使用这行代码时出现错误

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

错误输出是:

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.

我改变了

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

错误消失了,但我收到了这个错误:

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

这行导致错误:

const PRODUCTION = yargs.argv.prod;

有人有想法吗?

我已经尝试了很多并用谷歌搜索了这个错误,但我没有找到解决方案,有人对此有任何想法吗?提前致谢

标签: javascriptsassgulpgulp-sass

解决方案


您可以检查 package.json 文件中的“类型”部分吗?如果类型部分是模块,你会得到这个错误。你必须使它成为Commonjs。


推荐阅读