【问题标题】:ReferenceError: require is not defined in ES module scope, you can use import instead gulp sassReferenceError: require 没有在 ES 模块范围内定义,你可以使用 import 代替 gulp sass
【发布时间】:2021-11-05 00:46:39
【问题描述】:

使用这行代码时出现错误

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;

有人有想法吗?

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

【问题讨论】:

    标签: javascript sass gulp gulp-sass


    【解决方案1】:
    1. 首先将"type":"commonjs" 添加到package.json。
    2. 降级您的软件包版本。如果您使用的是 gulp-imagemin 版本 8.0.0,请安装旧版本,例如 npm i gulp-imagemin@7.1.0
      这就是现在享受编码的一切......

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • { “name”:“myfirsttheme”,“version”:“1.0.0”,“description”:“description”,“main”:“index.js”,“type”:“模块”,
      • 你的意思是“类型”:“commonjs”?
      • @askcoder 如果你使用require,类型应该是commonjs。
      • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
      • 我已经用新的细节更新了这个问题
      猜你喜欢
      • 2022-09-28
      • 2021-03-13
      • 2015-12-21
      • 2021-01-16
      • 2023-03-08
      • 2017-03-02
      • 2022-01-17
      • 2023-03-19
      • 2021-09-11
      相关资源
      最近更新 更多