javascript - 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;
有人有想法吗?
我已经尝试了很多并用谷歌搜索了这个错误,但我没有找到解决方案,有人对此有任何想法吗?提前致谢
解决方案
您可以检查 package.json 文件中的“类型”部分吗?如果类型部分是模块,你会得到这个错误。你必须使它成为Commonjs。
推荐阅读
- python - 无法使用 tensorflow 的构建错误访问 keras 模型的权重
- python - 如何实时更新 PyQt5 标签?
- c# - 如何在没有未知密钥的情况下访问 Json 数据?
- reactjs - 如何将所有路由重定向到 gatsby 索引
- python - 如何正确启用 keras fit() 多处理?
- c# - 带有 C# 的 ms-graph API;如何修复此驱动项错误
- javascript - React:如何在每个映射数组项上传递状态?
- python - TypeError:列表索引必须是整数或切片,而不是 BioPython 中的 str
- javascript - Angular json 值未显示在文本框中
- c# - 如何将光标放在 ASP.NET 文本框的开头以获得焦点?