Adding gulp-sass breaks by task runner - node.js

I have a basic gulp setup in VS2017 to minify my Javascript. I decided to add gulp-sass (my package.json says I'm on gulp-sass v4.0.1) but it throws this error:
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)
My gulpfile looks like this:
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']);
});
I did some Googling and a simple fix suggested was to add "use strict" to the top of the offending file, in this case index.js:66. However, after doing that I get:
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)
I am running Node.js v6. I'm lost as to why what should be a simple process is giving me these errors. What am I doing wrong?
Update:
I ran the following commands suggested in the comments:
npm install node-sass -f
npm rebuild nose-sass
Both ran successfully. However, I'm still getting this error:
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)
Update 2:
I was advised to add "use strict"; to the top of my gulpfile.js, but the same error occurs. Here's the file contents:
"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'));
});
Most common issue online appears to be Node.js version of < 6.0, but I'm running v6.11.1.
Update 3: (solved)
I finally found the cause & solution; I've added it as an answer down below for any future readers. Enjoy.

Managed to find the problem so I'm answering my own question for future readers.
Whilst I have node.js v6.11.1 installed, Visual Studio 2017 comes bundled with it's own version of node that it uses by default. Even if you run node -v in the VS2017 shell and it tells you it's running v6.11.1, it's actually - by default - running whatever it finds in .\node_mobules\.bin.
The solution is this:
In VS2017, go "Tools > Options > Projects and Solutions > Web Package Management > External Web Tools".
You'll probably see this:
Add the path to your standalone installation of node (default C:\Program Files\nodejs) and, using the arrows, position it above the .\node_modules\bin version, like this:
Hit OK and either refresh the Task Runner Explorer or restart VS2017. Your gulpfile should now build.

In my case, I had to move $(PATH) above $(VSINSTALLDIR)/Web/External to fix the problem.

Related

PM2 - SyntaxError: Block-scoped declarations not yet supported outside strict mode

I'm currently installing a NodeJS service on an Ubuntu server. The application is written in coffeescript and runs fine on it's own. This is how I setup the project:
cd ~/test-project
nvm use v4.9.1
npm install
pm2 start index.js
However when I try to start it using pm2 i get the error below:
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
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:416: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> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
And after doing some testing it seems that this error occurs no matter what script I start with pm2 so there must be something that isn't configured correctly..
I don't know if it matters but the script I am trying to run needs node v4.9.1 so I am using NVM to use that version, it seems to me that it is trying to run pm2 with that same node version somehow?
Any input is greatly appreciated as I've been stuck on this for way too long!!
I had the same issue on node version 4.x,
Downgraded the pm2 and it worked.
npm uninstall -g pm2
npm install -g pm2#3.0.4
'use strict';
Add above line of code to first line of the script file. let is used when you want a variable to be scoped to a block which is supported in strict mode in node v4.
See the documentation for more details.
#Vishnu Sing this is the complete output. The index.js file looks like this:
console.log('Hello World');
ubuntu#ip-172-31-32-5:~/test-project$ nano index.js
ubuntu#ip-172-31-32-5:~/test-project$ node -v
v10.15.3
ubuntu#ip-172-31-32-5:~/test-project$ sudo pm2 start index.js
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
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:416: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> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
ubuntu#ip-172-31-32-5:~/test-project$

command gulp error "assert.js:42 throw new errors.AssertionError"

I have a simple install gulp livereload:
gulp
assert.js:42
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (c:\testjses6\node_modules\undertaker\lib\set-task.js:10:3)
at Gulp.task (c:\testjses6\node_modules\undertaker\lib\task.js:13:8)
at Object. (c:\testjses6\gulpfile.js:21:6)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
The problem you are encountering is that you are running gulp v4.0 using gulp 3 syntax. The AssertionError: Task function must be specified error comes from the fact that gulp.task has changed and the 3 parameters signature was removed. In short, tasks that look like the one below will need to be changed to remove the [] parameter:
gulp.task('default', ['build'], function () {
return runSequence(['watch', 'karma-watch']);
});
This article will help you migrate to gulp 4 syntax.
If you'd like a simple fix to your problem, without having to make changes to your gulpfile.js, run the following command to downgrade your version of gulp from version 4 to 3.9.1:
npm install gulp#3.9.1 --save
Assuming all goes well you should no longer see the Assertion error.
gulp 4 creates many issues with old implementations, modify you tasks declarations:
Old syntax:
gulp.task('a', ['b', 'c'], function () { // do something })
New syntax:
gulp.task('a', gulp.series(gulp.parallel('b', 'c'), function () { // do something }))
Hope this helps someone, I figured it after spending hours troubleshooting.

ES 2015 VS 2015 for node.js running task runner explorer

I have a ASP.NET 5 project in VS 2015. I'm setting up my gulp tasks and I am using the gulp-chmod module. This allows me to remove read only properties set by TFS in my copy processes. I've used v1.3 of this module sucessfully before in VS 2015 however the new version v2.0 has been upgraded to use ES 2015 features specifically:
note use of const, let
'use strict';
const through = require('through2');
const deepAssign = require('deep-assign');
const Mode = require('stat-mode');
const defaultMode = 0o777 & (~process.umask());
function normalize(mode) {
let called = false;const through = require('through2');
const deepAssign = require('deep-assign');
...
I get the error:
cmd.exe /c gulp --tasks-simple
<MY_PATH>\node_modules\gulp-chmod\index.js:2
const through = require('through2');
^^^^^
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (<MYPATH>\gulpfile.js:9:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
In the output from Task Runner explorer.
I have the latest version of Node.js installed and off the command line it all works well.
My node path is set to point to ./node_modules/.bin
I checked in the package manager console using node -v and it appears I am using the latest node version.
So how do I control the Node version in use through task manager. I would like it to use ES2015 to compile my modules?
Thanks in advance
It sounds like you're running on an older version of Node.js.
I found this github ticket that suggests adding the following to your gulp script to show what version of node you're running under.
https://github.com/sindresorhus/gulp-imagemin/issues/178
console.log('Node version: ' + process.version);
You can go to Tools > Options > Projects and Solutions > External Web Tools to find out where the version of node that VS is using resides.
This question has a fairly elegant solution of moving your "$(PATH)" entry to the top: Gulp task failing when run from VS 2015 Task Runner explorer, but not from command prompt

Error running Gulpfile from Visual Studio 2015 task runner

I am building a gulpfile to use in my aps.net winforms project. So far I have the following in the package.json
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
"browserify": "^13.1.0",
"del": "2.2.0",
"gulp": "^3.9.1",
"gulp-sourcemaps": "^2.2.0",
"gulp-typescript": "^3.0.2",
"tsify": "^2.0.2",
"vinyl-source-stream": "^1.1.0"
}
}
and the gulp file has
/// <binding BeforeBuild='default' />
/*
This file in the main entry point for defining Gulp tasks and using Gulp plugins.
Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/
var gulp = require('gulp');
var del = require('del');
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
var sourcemaps = require('gulp-sourcemaps');
gulp.task('default', function () {
// place code for your default task here
});
My node version is 6.9.1
If I run this from the command line ('gulp') it works fine. But in the Visual Studio task runner, it fails to load, and in the out put I see the following error. This error starts after I add the line var sourcemaps = require('gulp-sourcemaps');
Failed to run "H:\dev\myproj\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
H:\dev\myproj\node_modules\gulp-sourcemaps\node_modules\strip-bom\index.js:2
module.exports = x => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (H:\dev\myproj\node_modules\gulp-sourcemaps\src\init.js:10:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Also adding var tsify = require("tsify"); I get another error...
Failed to run "H:\dev\myproject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
TypeError: Object #<Object> has no method 'debuglog'
at Object.<anonymous> (H:\dev\myproject\node_modules\tsify\index.js:4:32)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (H:\dev\myproject\gulpfile.js:17:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Anyone have any ideas why I am getting this?
Thanks in advance!
My problem was I didn't realize Visual Studio has it's own (way out of date) version of the node tools (installed within the Visual Studio folders somewhere). How untidy!
To make it use the "global" node tools, I could go to the menu Tools | Options | Projects and Solutions | External Web Tools and move the $(PATH) to be before the $(DevEnvDir)\* locations. After I did this, my Gulp file would load.
The other option may have been to update the Visual Studio node tools via the Tools | Extensions and Updates, but (I think) I'd prefer to use the same external tools I will use outside of Visual Studio.
After some investigate with me facing the same issue, it seems to be a problem with version compatibility between your nodejs install, and your package dependency "strip-bom".
An adhoc fix is to change from:
module.exports = x => {
to:
module.exports = function (x) {
You might want to check this out:
Gulp-Sourcemaps, SyntaxError: Unexpected token >
That seems to solve the issue for me, but not such a nice method.
The problem is that you might be running an old version of node. Try to use the command node -v or nodejs -v to check your version.
Though it might not be your case, but great for others to come by and see this, Debian/Ubuntu actually has an an outdated version of node on its package manager "apt-get". So it is highly possible that you're still running an old version such as 0.10.25.
The real fix I'd recommend (which I ended up doing), is to upgrade your node version. As of current date, version 6.9.1 seems to be the latest stable.
You can check the install guide at the nodejs official website here:
https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

fs undefined in cakefile

I'm trying to build a simple cakefile to perform build tasks for a node project I'm working on. Following this gist from github, I've managed to throw the following basic code together:
CoffeeScript = require 'coffee-script'
{exec} = require 'child_process'
fs = require 'fs'
web_build_path = 'bin/web'
task 'build', 'build server-side project code and output to bin dir', ->
makeUnrevisionedDirs()
#snip other methods...
makeUnrevisionedDirs = () ->
console.log(": adding unrevisioned directories...")
if not fs.existsSync(web_build_path)
console.log(":: adding #{web_build_path} dir")
fs.mkdirSync(web_build_path)
if not fs.existsSync('logs')
console.log(":: adding logs dir")
fs.mkdirSync('logs')
if not fs.existsSync('bin')
console.log(":: adding bin dir")
fs.mkdirSync('bin')
And get the following error, as if fs was never required:
TypeError: Cannot call method 'existsSync' of undefined
at makeUnrevisionedDirs (C:\fms\Cakefile:25:9, <js>:22:13)
at Object.makeUnrevisionedDirs [as action] (C:\fms\Cakefile:7:2, <js>:11:5)
at helpers.extend.invoke (C:\Users\Mike\AppData\Roaming\npm\node_modules\coffe
e-script\lib\coffee-script\cake.js:45:26)
at Object.exports.run (C:\Users\Mike\AppData\Roaming\npm\node_modules\coffee-s
cript\lib\coffee-script\cake.js:72:21)
at Object.<anonymous> (C:\Users\Mike\AppData\Roaming\npm\node_modules\coffee-s
cript\bin\cake:7:38)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:903:3
why is fs undefined here?
I had this problem and discovered I was running a really old version of node (0.6) - your require is failing quietly, fs must be a newer package that didn't exist.
In the end I installed the n package, upgraded node and all was well
How can I update Node.js and npm to the next versions?
(go right to the bottom of the answer for the 3 commands I used)
Note that I was also running Z shell and needed to tell the shell to rehash its list of executables with the rehash command before it found the newer version of node.

Resources