ask*_*der 52 javascript sass gulp gulp-sass
我在使用这行代码时遇到错误
const sass = require('gulp-sass')(require('sass'));
Run Code Online (Sandbox Code Playgroud)
错误输出是:
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.
Run Code Online (Sandbox Code Playgroud)
我变了
const sass = require('gulp-sass')(require('sass'));
Run Code Online (Sandbox Code Playgroud)
错误消失了,但我收到此错误:
Requiring external module babel-register
TypeError: Cannot read property 'prod' of undefined
Run Code Online (Sandbox Code Playgroud)
这行导致了错误:
const PRODUCTION = yargs.argv.prod;
Run Code Online (Sandbox Code Playgroud)
有人有想法吗?
我已经尝试了很多并用谷歌搜索了这个错误,但我没有找到解决方案,有人对此有想法吗?提前致谢
小智 18
您可以检查 package.json 文件中的“type”部分吗?如果类型部分是模块,您将收到此错误。你必须把它变成Commonjs。
pro*_*ype 14
虽然许多节点库都支持require和import,但我偶尔会遇到这样的情况:我需要使用的一个库不支持require,而另一个库也不支持import。
您可以使用在 ES6 模块中createRequire定义require并使用它来导入 Common JS 库。以下是如何使用它的示例代码。
import { createRequire } from "module";
const require = createRequire(import.meta.url);
import crypto from 'crypto'
const fs = require('fs')
Run Code Online (Sandbox Code Playgroud)
这个答案直接从此页面提取: https://dev.to/caspergeek/how-to-use-require-in-ecmascript-modules-1l42
| 归档时间: |
|
| 查看次数: |
202304 次 |
| 最近记录: |