Angular项目执行npm start遇ERR_REQUIRE_ESM错误求助
Angular项目重装Node.js和npm后执行
npm start出现ERR_REQUIRE_ESM错误 我的Angular项目原本运行正常,因其他问题重装Node.js和npm后出现异常,执行npm start命令时触发如下错误:
Error [ERR_REQUIRE_ESM]: require() of ES Module D:...\angular\node_modules\globby\index.js from D:...\angular\gulpfile.js not supported.
Instead change the require of index.js in D:...\angular\gulpfile.js to a dynamic import() which is available in all CommonJS modules.
at Object. (D:...\angular\gulpfile.js:4:14)
at async Promise.all (index 0) {
code: 'ERR_REQUIRE_ESM'
}
我尝试切换回旧版本Node.js和npm,问题仍未解决。当前环境版本:
D:\...\angular>node -v v17.7.0 D:\...\angular>npm -v 7.19.1
我参考了多篇同类问题的解决方案,但均无效。以下是我的gulpfile.js代码:
var gulp = require("gulp"); var path = require('path'); var merge = require("merge-stream"); var globby = require('globby'); var concat = require('gulp-concat'); var less = require('gulp-less'); var uglify = require('gulp-uglify'); var cleanCss = require('gulp-clean-css'); var bundleConfig = require(path.resolve(__dirname, 'bundles.json')); var production = false; var styleEntries = {}; var scriptEntries = {}; function processInputDefinition(input) { var result = []; for (var i = 0; i < input.length; i++) { var url = input[i]; if (url.startsWith('!')) { result.push('!' + path.resolve(__dirname, url.substring(1))); } else { result.push(path.resolve(__dirname, url)); } } return result; } function fillScriptBundles() { // User defined bundles for (var k = 0; k < bundleConfig.scripts.length; k++) { var scriptBundle = bundleConfig.scripts[k]; scriptEntries[scriptBundle.output] = globby.sync(processInputDefinition(scriptBundle.input), { noext: true }); } } function fillStyleBundles() { // User defined styles for (var k = 0; k < bundleConfig.styles.length; k++) { var styleBundle = bundleConfig.styles[k]; styleEntries[styleBundle.output] = globby.sync(processInputDefinition(styleBundle.input), { noext: true }); } } function getFileNameFromPath(path) { return path.substring(path.lastIndexOf('/') + 1); } function getPathWithoutFileNameFromPath(path) { return path.substring(0, path.lastIndexOf('/')); } function createScriptBundles() { var tasks = []; for (var script in scriptEntries) { tasks.push( createScriptBundle(script) ); } return tasks; } function createScriptBundle(script) { var bundleName = getFileNameFromPath(script); var bundlePath = getPathWithoutFileNameFromPath(script); var stream = gulp.src(scriptEntries[script]); if (production) { stream = stream .pipe(uglify()); } return stream.pipe(concat(bundleName)) .pipe(gulp.dest(bundlePath)); } function createStyleBundles() { var tasks = []; for (var style in styleEntries) { tasks.push( createStyleBundle(style) ); } return tasks; } function createStyleBundle(style) { var bundleName = getFileNameFromPath(style); var bundlePath = getPathWithoutFileNameFromPath(style); var stream = gulp.src(styleEntries[style]) .pipe(less({ math: 'parens-division' })); if (production) { stream = stream.pipe(cleanCss()); } return stream .pipe(concat(bundleName)) .pipe(gulp.dest(bundlePath)); } function build() { production = true; fillScriptBundles(); fillStyleBundles(); var scriptTasks = createScriptBundles(); var styleTasks = createStyleBundles(); return merge(scriptTasks.concat(styleTasks)); } function buildDev() { fillScriptBundles(); fillStyleBundles(); var scriptTasks = createScriptBundles(); var styleTasks = createStyleBundles(); console.log("Dynamic bundles are being created."); return merge(scriptTasks.concat(styleTasks)); } exports.build = build; exports.buildDev = buildDev;
内容的提问来源于stack exchange,提问作者Saba
相关产品推荐
相关产品推荐

