node.js - 通过任务运行器添加 gulp-sass 中断

标签 node.js sass gulp gulp-sass

我在 VS2017 中有一个基本的 gulp 设置来缩小我的 Javascript。我决定添加 gulp-sass(我的 package.json 说我在 gulp-sass v4.0.1 上)但是它抛出了这个错误:

C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
    let sassMap;
    ^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:11:12)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)

我的 gulpfile 看起来像这样:

var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var uglify = require('gulp-uglify');
var sass = require('gulp-sass');

gulp.task('minify', function () {
    gulp.src('src/**/*.js')
        .pipe(uglify({ mangle: false }))
        .pipe(concat('scripts.min.js'))
        .pipe(gulp.dest('Content'));


});

gulp.task('sass', function () {
    gulp.src('src/css/**/*.scss')
        .pipe(sass({ outputStyle: 'compressed' }))
        .pipe(gulp.dest('Content'));
});

gulp.task('watch', function () {
    gulp.watch('src/**/*.js', ['minify']);
});

我做了一些谷歌搜索,建议的一个简单修复是将“use strict”添加到有问题的文件的顶部,在本例中为 index.js:66。然而,这样做之后我得到:

Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15
      throw new Error(errors.missingBinary());
      ^
Error: Missing binding C:\Work\MyProject\MyProject\node_modules\node-sass\vendor\win32-x64-47\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 5.x
Found bindings for the following environments:
  - Windows 64-bit with Node.js 6.x
This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass --force` to build the binding for your current environment.
    at module.exports (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15:13)
    at Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\index.js:14:35)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:163:21)
    at Module._compile (module.js:397:26)

我正在运行 Node.js v6。我不知道为什么应该是一个简单的过程却给我这些错误。我做错了什么?

更新:

我运行了评论中建议的以下命令:

npm install node-sass -f
npm rebuild nose-sass

两者都运行成功。但是,我仍然收到此错误:

Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
    let sassMap;
    ^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:8:12)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)

更新 2:

有人建议我添加“use strict”;到我的 gulpfile.js 的顶部,但出现同样的错误。这是文件内容:

"use strict";
var gulp = require('gulp');
var sass = require('gulp-sass'); // If I comment this out, I can build

gulp.task('sass', function () {
    gulp.src('src/css/**/*.scss')
        .pipe(sass({ outputStyle: 'compressed' }))
        .pipe(gulp.dest('Content'));
});

enter image description here

网上最常见的问题似乎是 Node.js 版本 < 6.0,但我运行的是 v6.11.1。

更新 3:(已解决)

终于找到了原因和解决方案;我已将其添加为下面的任何 future 读者的答案。享受吧。

最佳答案

设法找到了问题,所以我正在为 future 的读者回答我自己的问题。

虽然我安装了 node.js v6.11.1,但 Visual Studio 2017 捆绑了它默认使用的自己的 Node 版本。即使你运行 node -v在 VS2017 shell 中它告诉你它正在运行 v6.11.1,它实际上 - 默认情况下 - 运行它在 .\node_mobules\.bin 中找到的任何内容.

解决方案是这样的:

  1. 在 VS2017 中,转到“工具 > 选项 > 项目和解决方案 > Web 包管理 > 外部 Web 工具”。

  2. 您可能会看到:

enter image description here

  1. 将路径添加到您的独立安装 Node (默认 C:\Program Files\nodejs ),并使用箭头将其定位在 .\node_modules\bin 之上版本,像这样:

enter image description here

  1. 点击确定并刷新 Task Runner Explorer 或重新启动 VS2017。你的 gulpfile 现在应该构建了。

enter image description here

关于node.js - 通过任务运行器添加 gulp-sass 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014573/

相关文章:

node.js - Express 框架或 node.js 如何自动提供 javascript 文件?

javascript - 使用 babel 时 Nodemon 在保存时运行多次

sass - Normalise.css 与波本苦酒一起

laravel-5 - vue app 对象不是对象类型

javascript - 使用 Gulp.js 模板编译器渲染 HTML

node.js - 为什么我不能部署我的应用程序? (蓝调)

node.js - 类型错误 : fs/promises read is not a function

css - CSS3 过渡流程问题

css - 标准 yeoman webapp 设置中的 compass 无法正确呈现线性渐变

node.js - Gulp/Bower 错误