简体   繁体   English

Gulp 抛出错误“必须使用导入来加载 ES 模块”

[英]Gulp throws error 'Must use import to load ES Module'

We're converting our gulpfile.js in node v13.8.0 to ES6 as following:我们将我们的gulpfile.js在节点v13.8.0到ES6如下:

import { src, dest, series, parallel, watch } from 'gulp'
import nodemon from 'gulp-nodemon' // normal nodemon does not display an error on app crash
import env from 'gulp-env'
import browser from 'browser-sync'
import sass from 'gulp-sass'
// Tasks
export default {
    cssTranspile: cssTranspile,
    jsTranspile: jsTranspile,
    server: series(startNodemon, startBrowserSync),
    default: series(
        parallel(
            cssTranspile,
            jsTranspile
        ),
        startNodemon,
        startBrowserSync,
        function () {
            watch('public/scss/*.scss', cssTranspile)
        }
    )
}

The error reported, when simply running gulp , is:简单地运行gulp时报告的错误是:

internal/modules/cjs/loader.js:1160
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: T:\Node\ICP\gulpfile.js
require() of ES modules is not supported.

I must be doing something wrong.我一定做错了什么。 Anyone an idea on what it might be?任何人都知道它可能是什么?

CLI version: 2.2.0
Local version: 4.0.2

The flag "type": "module", is set in package.json as described in the note docs .标记"type": "module",package.json设置,如注释文档中所述 The issue is very much similar to this issue in the geolib library.这个问题与 geolib 库中的这个问题非常相似。

And when we rename gulpfile.js to gulpfile.babel.js as described here we get the same error.当我们按照这里的描述将gulpfile.js重命名为gulpfile.babel.js ,我们会得到同样的错误。

The package.json contain these packages: package.json包含这些包:

  "devDependencies": {
    "@babel/core": "^7.8.4",
    "@babel/preset-env": "^7.8.4",
    "@babel/register": "^7.8.3",
    "exports-loader": "^0.7.0",
    "gulp": "^4.0.2",
    "gulp-env": "^0.4.0",
    "gulp-nodemon": "^2.4.2",
    "gulp-sass": "^4.0.2",
    "nodemon": "^2.0.2"
  },
  "type": "module",
  "babel": {
    "presets": [
      "@babel/env"
    ]

In an answer to my own question, when the flag "type": "module" is set in package.json you can't use gulp .在回答我自己的问题时,当在 package.json 中设置标志"type": "module" ,您不能使用gulp More info can be found here .更多信息可以在这里找到。

This seems to be fixed now: https://github.com/gulpjs/gulp-cli/pull/214 .现在这似乎已修复: https : //github.com/gulpjs/gulp-cli/pull/214 Be sure to install the latest version of gulp-cli (2.3.0 as of June 2020).请务必安装最新版本的gulp-cli (截至 2020 年 6 月为 2.3.0)。

If your package.json specifies "type": "module" you can name your ES module with Gulp tasks gulpfile.js , otherwise name it gulpfile.mjs .如果你的 package.json 指定了"type": "module"你可以用 Gulp 任务gulpfile.js命名你的 ES 模块,否则命名为gulpfile.mjs No need to use any transpilers or preloaders like esm, Babel or TypeScript.无需使用任何转译器或预加载器,如 esm、Babel 或 TypeScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM