我在使用这行代码时出错了
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。
为我工作,只更改 package.json 文件,这一行:
“类型”:“模块” 到 “类型”:“commonjs”
这个错误是因为 require 是 CommonJS 模块语法,而 import 是 ES6 模块语法。要解决这个问题,你应该使用 import 而不是 require ;在你的 json 包中,类型必须包含 "type": "module" 如果你使用的是 Node js
"type":"commonjs"
到package.json.npm i [email protected]
.