Задать вопрос
Fox7777
@Fox7777
Люблю python

Как исправить ошибку TypeError: Cannot read properties of undefined (reading 'emit')?

Выводит ошибку сразу после добавления в файл .js кода.
import Swiper from 'swiper';
import 'swiper/css';
document.addEventListener('DOMContentLoaded', () => {
	const swiperIMG = new Swiper('.slider-img',{
		loop: false,
		speed: 2400,
	})

})

это код js,
let preprocessor = 'sass', // Preprocessor (sass, less, styl); 'sass' also work with the Scss syntax in blocks/ folder.
		fileswatch   = 'html,htm,txt,json,md,woff2' // List of files extensions for watching & hard reload

import pkg from 'gulp'
const { gulp, src, dest, parallel, series, watch } = pkg

import browserSync   from 'browser-sync'
import bssi          from 'browsersync-ssi'
import ssi           from 'ssi'
import webpackStream from 'webpack-stream'
import webpack       from 'webpack'
import TerserPlugin  from 'terser-webpack-plugin'
import gulpSass      from 'gulp-sass'
import dartSass      from 'sass'
import sassglob      from 'gulp-sass-glob'
const  sass          = gulpSass(dartSass)
import less          from 'gulp-less'
import lessglob      from 'gulp-less-glob'
import styl          from 'gulp-stylus'
import stylglob      from 'gulp-noop'
import postCss       from 'gulp-postcss'
import cssnano       from 'cssnano'
import autoprefixer  from 'autoprefixer'
import imagemin      from 'gulp-imagemin'
import changed       from 'gulp-changed'
import concat        from 'gulp-concat'
import rsync         from 'gulp-rsync'
import {deleteAsync} from 'del'

// function browsersync() {
// 	browserSync.init({
// 		proxy: "COOL-SLIDER",
// 		ghostMode: { clicks: false },
// 		notify: false,
// 		online: true,
// 		// tunnel: 'yousutename', // Attempt to use the URL https://yousutename.loca.lt
// 	})
// }

function browsersync() {
	browserSync.init({
		server: {
			baseDir: 'app/',
			middleware: bssi({ baseDir: 'app/', ext: '.html' })
		},
		ghostMode: { clicks: false },
		notify: false,
		online: true,
		open: true,
		// tunnel: 'yousutename', // Attempt to use the URL https://yousutename.loca.lt
	})
}

function scripts() {
	return src(['app/js/*.js', '!app/js/*.min.js'])
		.pipe(webpackStream({
			mode: 'production',
			performance: { hints: false },
			plugins: [
				new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', 'window.jQuery': 'jquery' }), // jQuery (npm i jquery)
			],
			module: {
				rules: [
					{
						test: /\.m?js$/,
						exclude: /(node_modules)/,
						use: {
							loader: 'babel-loader',
							options: {
								presets: ['@babel/preset-env'],
								plugins: ['babel-plugin-root-import']
							}
						}
					}
				]
			},
			optimization: {
				minimize: true,
				minimizer: [
					new TerserPlugin({
						terserOptions: { format: { comments: false } },
						extractComments: false
					})
				]
			},
		}, webpack)).on('error', (err) => {
			this.emit('end')
		})
		.pipe(concat('app.min.js'))
		.pipe(dest('app/js'))
		.pipe(browserSync.stream())
}

function styles() {
	return src([`app/styles/${preprocessor}/*.*`, `!app/styles/${preprocessor}/_*.*`])
		.pipe(eval(`${preprocessor}glob`)())
		.pipe(eval(preprocessor)({ 'include css': true }))
		.pipe(postCss([
			autoprefixer({ grid: 'autoplace' }),
			cssnano({ preset: ['default', { discardComments: { removeAll: true } }] })
		]))
		.pipe(concat('app.min.css'))
		.pipe(dest('app/css'))
		.pipe(browserSync.stream())
}

function images() {
	return src(['app/images/src/**/*'])
		.pipe(changed('app/images/dist'))
		.pipe(imagemin())
		.pipe(dest('app/images/dist'))
		.pipe(browserSync.stream())
}

function buildcopy() {
	return src([
		'{app/js,app/css}/*.min.*',
		'app/images/**/*.*',
		'!app/images/src/**/*',
		'app/fonts/**/*'
	], { base: 'app/' })
	.pipe(dest('dist'))
}

async function buildhtml() {
	let includes = new ssi('app/', 'dist/', '/**/*.html')
	includes.compile()
	await deleteAsync('dist/parts', { force: true })
}

async function cleandist() {
	await deleteAsync('dist/**/*', { force: true })
}

function deploy() {
	return src('dist/')
		.pipe(rsync({
			root: 'dist/',
			hostname: 'username@yousite.com',
			destination: 'yousite/public_html/',
			// clean: true, // Mirror copy with file deletion
			include: [/* '*.htaccess' */], // Included files to deploy,
			exclude: [ '**/Thumbs.db', '**/*.DS_Store' ],
			recursive: true,
			archive: true,
			silent: false,
			compress: true
		}))
}

function startwatch() {
	watch(`app/styles/${preprocessor}/**/*`, { usePolling: true }, styles)
	watch(['app/js/**/*.js', '!app/js/**/*.min.js'], { usePolling: true }, scripts)
	watch('app/images/src/**/*', { usePolling: true }, images)
	watch(`app/**/*.{${fileswatch}}`, { usePolling: true }).on('change', browserSync.reload)
}

export { scripts, styles, images, deploy }
export let assets = series(scripts, styles, images)
export let build = series(cleandist, images, scripts, styles, buildcopy, buildhtml)

export default series(scripts, styles, images, parallel(browsersync, startwatch))

это код gulpfile.js,
scripts' errored after 1.86 s
[20:25:38] TypeError: Cannot read properties of undefined (reading 'emit')
    at Stream.<anonymous> (file:///mnt/d/programming/%D0%BA%D1%83%D1%80%D1%81%D1%8B/%D0%B3%D0%BE%D1%82%D0%BE%D0%B2%D1%8B%D0%B5%20%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D1%8B/cool-slider/gulpfile.js:87:9)       
    at Stream.emit (node:events:525:35)
    at Stream.emit (node:domain:489:12)
    at callback (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack-stream/index.js:189:16)
    at finalCallback (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compiler.js:441:32)
    at /mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compiler.js:458:13
    at Hook.eval [as callAsync] (eval at create (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/tapable/lib/Hook.js:18:14)
    at onCompiled (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compiler.js:456:21)
    at /mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compiler.js:1196:17
    at Hook.eval [as callAsync] (eval at create (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/tapable/lib/Hook.js:18:14)
    at /mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compiler.js:1192:33
    at finalCallback (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compilation.js:2787:11)
    at /mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compilation.js:3092:11
    at Hook.eval [as callAsync] (eval at create (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/tapable/lib/Hook.js:18:14)
    at /mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/webpack/lib/Compilation.js:3085:38
    at eval (eval at create (/mnt/d/programming/курсы/готовые проекты/cool-slider/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:17:1)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

вот и ошибка
  • Вопрос задан
  • 784 просмотра
Подписаться 1 Средний 4 комментария
Пригласить эксперта
Ответы на вопрос 2
@furashcka
Скорее всего проблема в "this.emit('end')", this ссылается на функцию scripts, так? А в prototype функции scripts есть функция emit? Наверняка при выводе console.log(this.emit); вы увидите undefined
Ответ написан
Комментировать
@Digital_worm
У вас получилось исправить ошибку?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы