Node.js和Gulp协助

时间:2018-07-13 14:06:02

标签: node.js gulp

早上好,我是Gulp和Node Js的新手,我一直收到此错误(如下所示),我进行了一些研究,但没有找到任何修复程序。

 [Browserslist] Could not parse /Users/gendyblackman/Sites/blackmanimages/package.json. Ignoring it.

assert.js:269     抛出错误;

AssertionError [ERR_ASSERTION]:任务从未定义:干净

at getFunction (/Users/gendyblackman/Sites/blackmanimages/node_modules/undertaker/lib/helpers/normalizeArgs.js:15:5)
at map (/Users/gendyblackman/Sites/blackmanimages/node_modules/arr-map/index.js:20:14)
at normalizeArgs (/Users/gendyblackman/Sites/blackmanimages/node_modules/undertaker/lib/helpers/normalizeArgs.js:22:10)
at Gulp.series (/Users/gendyblackman/Sites/blackmanimages/node_modules/undertaker/lib/series.js:13:14)
at Object.<anonymous> (/Users/gendyblackman/Sites/blackmanimages/gulpfile.js:115:9)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)

此外,当我在本地重新安装Gulp时,出现以下错误。

npm ERR! file /Users/gendyblackman/Sites/blackmanimages/package.json

npm ERR! code EJSONPARSE

npm ERR! JSON.parse Failed to parse json

npm ERR! JSON.parse Unexpected string in JSON at position 734 while parsing '{

npm ERR! JSON.parse "name": "blackmanimages",

npm ERR! JSON.parse "version'

npm ERR! JSON.parse Failed to parse package.json data.

npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

npm ERR! A complete log of this run can be found in:

npm ERR! /Users/gendyblackman/.npm/_logs/2018-07-05T14_56_20_043Z-debug.log

任何帮助都会很棒。 谢谢,

另外,这是我的Gulpfile.js

// Compiles all SASS files
gulp.task('scss', function () {
      return gulp.src('source/scss/**/*.scss')
    .pipe(plumber())
    .pipe(scss({
        style: 'compressed',
      }))
    .pipe(rename({
        basename: 'main',
        suffix: '.min',
      }))

    .pipe(gulp.dest('build/assets/css'))
    .pipe(browserSync.stream());
    });



//Optimizing Images
gulp.task('images', function (){
    return gulp.src('source/img/**/*.+(png|jpg|gif|svg)')
    .pipe(cache(imagemin({
      interlaced: true
    })))
    .pipe(gulp.dest('build/assets/img'))
  });

// Uglify js files
 gulp.task('scripts', function () {
    gulp.src('source/js/*.js')
    .pipe(plumber())
    .pipe(uglify())
    .pipe(gulp.dest('build/assets/js'))
    .pipe(browserSync.stream());
  });

//Concat and Compress Vendor .js files
 gulp.task('vendors', function () {
          gulp.src(
        [
            'source/js/vendors/jquery.min.js',
            'source/js/vendors/*.js',
        ])
    .pipe(plumber())
    .pipe(concat('vendors.js'))
    .pipe(uglify())
    .pipe(gulp.dest('build/assets/js'));
        });

// Watch for changes
gulp.task('watch', function () {
  //Serve files from the root of this project
    browserSync.init({
      server: {
          baseDir: "./build",
            },
      notify: false
      });

  gulp.watch(styleSrc, ['scss']);
  gulp.watch(scriptSrc, ['scripts']);
  gulp.watch(vendorSrc, ['vendors']);
  gulp.watch(['build/*.html', 'build/assets/css/*.css',   'build/assets/js/*.js', 'build/assets/js/vendors/*.js' , '/build/assets/img/*' , '/build/assets/img/'])
.on(' change ', browserSync.reload);

  });

//use default task to launch Browsersync and watch JS files
 gulp.task('default',gulp.series('clean',gulp.parallel('scss', 'scripts', 'vendors',`images`,`fonts`, 'watch'),      function (){
  console.log('Building files');
}));



gulp.task('default', [ 'sass', 'scripts', 'vendors', 'watch'], function () );


gulp.task('default', function (callback) {runSequence(['sass','browserSync', 'watch'],callback)});

package.json

{
    "name": "blackmanimages",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },

    "author": "",
    "license": "ISC",
    "devDependencies": {
        "browser-sync": "^2.24.5",
        "del": "^3.0.0",
        "express": "^4.16.3",
        "gulp": "github:gulpjs/gulp#4.0",
        "gulp-autoprefixer": "^5.0.0",
        "gulp-cache": "^1.0.2",
        "gulp-compass": "^2.1.0",
        "gulp-imagemin": "^4.1.0",
        "gulp-mamp": "0.0.5",
        "gulp-minify-css": "^1.2.4",
        "gulp-plumber": "^1.2.0",
        "gulp-rename": "^1.2.3",
        "gulp-sass": "^4.0.1",
        "gulp-sourcemaps": "^2.6.4",
        "minimatch": "^3.0.2",
        "mysql": "^2.15.0",
        "run-sequence": "^2.2.1"
        "gulp-concat": "^2.6.1",
        "gulp-contrib-copy": "0.1.3",
        "gulp-cssnano": "^2.1.3",
        "gulp-scss": "^1.4.0",
        "gulp-uglify": "^3.0.0",
        "gulp-useref": "^3.1.5"
    },
    "dependencies": {}
}

1 个答案:

答案 0 :(得分:1)

错误:

JSON.parse package.json must be actual JSON

您的package.json是正确格式的JSON。

检查此行:

"run-sequence": "^2.2.1"

在此行的末尾添加,

您没有定义任务clean,但您将其称为:

gulp.task('default',gulp.series('clean',gulp.parall .....

您应该删除它或创建clean

require('gulp-cache'),但已将其从package.json中删除。

检查环境变量NODE_ENVproduction吗? 如果为true,请将其删除。 (因为如果devDependenciesNODE_ENV,则不会安装production