VS 2019 gruntfile.js no tasks found when using 'sass = require('node-sass')' - gruntjs

I'm trying to set up grunt-sass for the first time on a new .Net Core 3.1 web app. I've gone through MSFT's steps to add grunt to a project here outlined here and then modified it with the steps from the grunt-sass instructions here.
This however causes task runner explorer to state there are no tasks found.
Here is my package.json:
{
"name": "chapelstudios_www",
"version": "0.0.2",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://me#bitbucket.org/me/chapelstudios_www.git"
},
"author": "mr",
"license": "ISC",
"homepage": "https://bitbucket.org/me/chapelstudios_www#readme",
"private": true,
"devDependencies": {
"grunt": "^1.0.4",
"grunt-cli": "^1.3.2",
"grunt-sass": "^3.1.0",
"node-sass": "^4.13.1"
},
"dependencies": {
"gulp": "^4.0.2"
}
}
And this is my gruntfile.js:
const sass = require('node-sass');
require('load-grunt-tasks')(grunt);
module.exports = function (grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Sass
sass: {
options: {
implementation: sass,
sourceMap: true, // Create source map
outputStyle: 'compressed' // Minify output
},
dist: {
files: [
{
expand: true, // Recursive
cwd: "Styles", // The startup directory
src: ["**/*.scss"], // Source files
dest: "wwwroot/css", // Destination
ext: ".css" // File extension
}
]
}
}
});
// Load the plugin
grunt.loadNpmTasks('grunt-sass');
// Default task(s).
grunt.registerTask('default', ['sass']);
};
I'm not sure how to get any more detailed error info then that but have tracked down the issue to the
const sass = require('node-sass');
line that is required by the grunt-sass instructions. If I change it to the string 'sass' that is recommended by older tutorials the task shows but fails when I attempt to actually run it.
I've also ran the following installations from an elevated powershell window in the project directory in an attempt to make sure they were installed into the project locally as I hear that to be a main issue:
npm install
npm install grunt
npm install grunt-cli
npm install --save-dev node-sass grunt-sass
At this point I'm out of ideas but I'm a newb so I'm sure I'm missing something obvious.

For anyone else visiting this with an existing project, I was having this issue with a pre-existing node-sass / grunt file on a new computer, and I found that bumping up the version of node-sass in my package.json caused VS to reinstall the packages and update the bindings as noted in the other answer.
I have a slight suspicion that there's a difference in versions between running grunt in my command prompt and whatever VS uses, since my grunt file worked just fine if I ran it manually, but would not show up in Task Runner Explorer.

Reviving an old topic but here is what worked for me. YMMV.
In Visual Studio, go to: Tools -> Options -> Web Package Management
-> External Web Tools.
Move $(PATH) above $(VSInstalledExternalTools).
I have no idea if there are any side effects to doing this.
credit to: https://developercommunity.visualstudio.com/solutions/314606/view.html

I found the following error which led me to do a rebuild:
Error: Missing binding A:\Projects\Repos\chapelstudios_www\node_modules\node-sass\vendor\win32-x64-79\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 13.x
Found bindings for the following environments:
- Windows 64-bit with Node.js 10.x
This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass` to download the binding for your current environment.

As Jake mentioned I tracked the error down to this line as well:
const sass = require('node-sass');
Running rebuild didn't work. I found that after upgrading node from version 10.x to current version (12.18 Windows) and then rebuilding fixed the issue.
npm rebuild node-sass

Related

asp.net 5, exclude .js files for typescript files

Is there a way to prevent the generated .js-files from being added to the solution when adding / saving a typescript file? I'm using Visual Studio 2015 CTP6 and ASP.NET 5.
-Thanks!
There is but it will prevent typescript files from being compiled on save.
Add the following to your .kproj file:
<PropertyGroup>
<TypeScriptCompileOnSaveEnabled>False</TypeScriptCompileOnSaveEnabled>
</PropertyGroup>
You can use grunt to produce js files from your ts since ts files will no longer be compiled on save.
Add grunt-ts to your package.json file
"grunt-ts": "^3.0.0"
Your gruntfile.js might look something like this:
ts: {
dev: {
src: ['app/src/ts/**/*.ts'],
outDir: 'ts-bin',
options: {
fast: 'never',
sourceRoot: 'js/app',
verbose: true}
}
},
Documentation for grunt-ts is here: https://www.npmjs.com/package/grunt-ts
One last tip: In VS2015, you can specify a grunt task that will trigger after builds using either the Task Runner Explorer in the GUI or by editing your project.json to include something like this:
"scripts": {
"postrestore": [ "npm install" ],
"prepare": [ "grunt bower:install" ],
"postbuild": [ "grunt default" ]
}
Now your TS files will be compiled into JS on every build.

Trouble updating packages using Bower in ASP .NET vNext

I have started ASP .NET vNext and I was going through several articles about using bower in Visual Studio 2015 for managing client side libraries, it's pretty simple to use but I am having problems in updating the packages...
I am following this
bower.json:
"dependencies": {
"bootstrap": "3.3.2",
"jquery": "1.4.1",
"jquery-validation": "1.11.1",
"jquery-validation-unobtrusive": "3.2.2",
"hammer.js": "2.0.4",
"bootstrap-touch-carousel": "0.8.0",
"jquery-migrate-official": "^1.2.1",
"bootstrap-hover-dropdown": "2.1.3",
"jquery-slimscroll": "1.3.3",
"jquery-cookie": "1.4.1",
"jquery.uniform": "4.3.0",
"blockui": "2.1.2",
"font-awesome": "4.3.0"
},
The intellisense says that the package blockui has the latest stable version 2.1.2 but package manager log says:
bower blockui#2.1.2 ENORESTARGET No tag found that was able to satisfy 2.1.2
bower blockui#2.1.2 not-cached git://github.com/malsup/blockui.git#2.1.2
bower blockui#2.1.2 resolve git://github.com/malsup/blockui.git#2.1.
Questions:
What does this mean? Is the intellisense picking up the wrong latest version?
Is there any better way to update all client side packages like I used to do using nuget package manager ?
update-package
I read that for server-side packages ASP .NET vNext will use nuget packages but when I write any command in my Package Manager Console nothing happens
update-package
install-package entityframework
You can right-click the 'Bower' node under the 'Dependencies' node and choose 'Restore Packages'. I often have to open the 'Task Runner Explorer' from 'View -> Other Windows' and run the 'bower' task from its context menu. This will trigger a bower installation.
gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
bower: {
install: {
options: {
targetDir: "wwwroot/lib",
layout: "byComponent",
cleanTargetDir: false
}
}
}
});
// This command registers the default task which will install bower packages into wwwroot/lib
grunt.registerTask("default", ["bower:install"]);
// The following line loads the grunt plugins.
// This line needs to be at the end of this this file.
grunt.loadNpmTasks("grunt-bower-task");
};
package.json
{
"version": "0.0.0",
"name": "",
"devDependencies": {
"grunt": "0.4.5",
"grunt-bower-task": "0.4.0"
}
}
The packages should then show up in your wwwroot -> lib directory.
Check out bower's website for more details: http://bower.io/
I just found that for (and for font-awesome package) IntelliSense advices non-existing version. That was 2.1.2, exactly like for your case, that is why I found your question.
I had re-checked real version, corrected it to existing one, and everything started to work.

How to compile .less files on save in Visual Studio 2015 (preview)

Ok, so I've created a new ASP.Net 5/MVC 6 project in Visual Studio 2015 Preview. In keeping with our current method of doing things, for styling I want to use .less files. Creating the files is straightforward, but Web Essentials no longer compiles them.
So my question is this: what precisely do I need to do to get my .css files generated when I save the .less files?
Based on my adventures getting Typescript to work nicely, I will have to use Grunt to accomplish this task, but I am brand-new to Grunt and so I'm not sure how one would do it?
Please help!
With VS 2015 Web Essential is split into multiple extensions you can download
the Web Compiler extension from here and it also has details on how to use it.
It is certainly not elegant as it used to be, but if you are using existing project and want to use a compiler for LESS then this may do the basic job.
So here's how to do it (compile on build and non-elegant compile on save):
Step 1
Open up your package.json file (it's in the root of your project) and add these lines:
"grunt-contrib-less": "^1.0.0",
"less": "^2.1.2"
Obviously you can change the version numbers (you'll get helpful intellisense), these are just the current versions.
Step 2
Right-click on the NPM folder (under Dependencies) and click Restore Packages. This will install less and grunt-contrib-less.
Step 3
Once those packages are restored, go to your gruntfile.js file (again, in the root of the project). Here, you'll need to add the following section to grunt.initConfig
less: {
development: {
options: {
paths: ["importfolder"]
},
files: {
"wwwroot/destinationfolder/destinationfilename.css": "sourcefolder/sourcefile.less"
}
}
}
You'll also need to add this line near the end of gruntfile.js:
grunt.loadNpmTasks("grunt-contrib-less");
Step 4
Then just go to View->Other Windows->Task Runner Explorer in the menu hit the refresh icon/button, then right-click on less under Tasks and go to Bindings and tick After Build.
Hooray, now less files will compile and we (I) learned about grunt, which seems really powerful.
Step 5: Compiling on save
I still haven't got this working to my satisfaction, but here's what I've got so far:
As above, add another NPM package grunt-contrib-watch (add to package.json, then restore packages).
Then add a watch section in gruntfile.js, like this (obviously this can work for other types of files as well):
watch: {
less: {
files: ["sourcefolder/*.less"],
tasks: ["less"],
options: {
livereload: true
}
}
}
So you'll now have something like this in your gruntfile.js:
/// <binding AfterBuild='typescript' />
// This file in the main entry point for defining grunt tasks and using grunt plugins.
// Click here to learn more. http://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409
module.exports = function (grunt) {
grunt.initConfig({
bower: {
install: {
options: {
targetDir: "wwwroot/lib",
layout: "byComponent",
cleanTargetDir: false
}
}
},
watch: {
less: {
files: ["less/*.less"],
tasks: ["less"],
options: {
livereload: true
}
}
},
less: {
development: {
options: {
paths: ["less"]
},
files: {
"wwwroot/css/style.css": "less/style.less"
}
}
}
});
// This command registers the default task which will install bower packages into wwwroot/lib
grunt.registerTask("default", ["bower:install"]);
// The following line loads the grunt plugins.
// This line needs to be at the end of this this file.
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-less");
grunt.loadNpmTasks("grunt-contrib-watch");
};
One can then simply set this task to run on Project Open (right-click on watch under Tasks in the Task Runner Explorer (it's under View->Other Windows in the top menu) and you're done. I would expect you'd have to close and re-open the project/solution to get this to kick in, otherwise you can manually run the task.
(Note: there is now a new question asked here directly concerning sass. I tried to alter the question and tags in this question to include sass, but someone didn't allow it.)
I would like to add the answer to the same question for sass (.scss). The answer is so related I think these may best be combined as two answers in this same post (if you disagree, please let me know; else, we might add "or sass" in the post title?). As such, see Maverick's answer for some fuller details, here's the nutshell for sass:
(Pre-step for Empty Projects)
If you started with an empty project, first add Grunt and Bower:
Right click solution -> Add -> 'Grunt and Bower to Project' (then wait for a minute for it to all install)
package.json:
"devDependencies": {
"grunt": "^0.4.5",
"grunt-bower-task": "^0.4.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-contrib-sass": "^0.9.2"
}
(FYI: grunt-contrib-sass link)
Then:
Dependencies -> right-click NPM -> Restore Packages.
gruntfile.js
1) Add or make sure these three lines are registered near the bottom (as NPM tasks):
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-sass");
2) Again in gruntfile.js, add init configurations, something like the following.
{ Caveat: I am no expert on such configurations. I found the sass configuration on an excellent blog post some time ago that I can't locate at this time in order to give credit. The key was I wanted to find all files in the project within a certain folder (plus descendants). The following does that (notice "someSourceFolder/**/*.scss", and see important related note here). }
// ... after bower in grunt.initConfig ...
"default": {
"files": [
{
"expand": true,
"src": [ "someSourceFolder/**/*.scss" ],
"dest": "wwwroot/coolbeans", // or "<%= src %>" for output to the same (source) folder
"ext": ".css"
}
]
},
"watch": {
"sass": {
"files": [ "someSourceFolder/**/*.scss" ],
"tasks": [ "sass" ],
"options": {
"livereload": true
}
}
}
Now follow the instructions for Task Runner Explorer as given in the other answer. Make sure to close and reopen project. It seems you have to run (double click) 'watch' (under 'Tasks') every time the project is started to get the watch watching, but then it works on subsequent saves.

Reloading/loading Grunt tasks after grunt-check-dependencies installs a Grunt task module

I have been trying to use grunt-check-dependencies to check for NPM module dependencies before running further Grunt tasks. The problem is that this doesn't help when one of the missing NPM dependencies is a Grunt task, e.g. grunt-contrib-concat.
Since Grunt apparently registers tasks before the 'checkDependencies' task is run, even though 'checkDependencies' will successfully install any missing Grunt task modules, it does this "too late" -- Grunt has already determined that the missing modules are missing, and the Grunt build run therefore fails.
One approach that will work is to always use a command line of e.g. "npm install; grunt ...", but I thought I would see if anyone has a completely 'Grunt-internal' solution.
Here's an example Grunt output that demonstrates the issue.
% rm -rf node_modules/grunt-contrib-concat ; grunt
>> Local Npm module "grunt-contrib-concat" not found. Is it installed?
Running "checkDependencies:this" (checkDependencies) task
>> grunt-contrib-concat: not installed!
Invoking npm install...
npm http GET https://registry.npmjs.org/grunt-contrib-concat
npm http 304 https://registry.npmjs.org/grunt-contrib-concat
grunt-contrib-concat#0.3.0 node_modules/grunt-contrib-concat
Warning: Task "concat:dev" not found. Use --force to continue.
Aborted due to warnings.
Here is the example gruntfile.js that caused the above output/error:
module.exports = function(grunt) {
'use strict';
var config = {
concat: {
dev: {
src: ['foo1.js', 'foo2.js'],
dest: 'foo_concat.js'
}
},
checkDependencies: {
this: {
options: {
npmInstall: true
},
},
}
};
// trying to make sure all npm nodules listed in package.json are
// installed before other grunt tasks
grunt.loadNpmTasks('grunt-check-dependencies');
grunt.task.run('checkDependencies');
// load grunt tasks
require('load-grunt-tasks')(grunt);
// alternate approach to loading tasks; exhibits same problem
// grunt.loadNpmTasks('grunt-contrib-concat');
grunt.initConfig(config);
grunt.registerTask('default', ['concat:dev']);
}

My grunt-regarde task is aborted if jshint detects an error

I expected grunt-regarde to go on watching for changes after a task is called.
This way I have to restart it (>grunt regarde) on the console each time a lint error was found.
Is grunt regarde supposed to behave like this or is this an issue with grunt-contrib-jshint and grunt-regarde?
Does this have something to do with the spawn option?
i was using grunt-contrib-watch before and it continued watching after jshint detected an error, which is want I expect regarde to do as well.
The reason why I started using regarde is that regard allows to just lint the changed file not the whole project, which is faster.
Here's my configuration:
grunt.initConfig({
jshint: {
file: '<%= grunt.regarde.changed %>'
},
regarde: {
javascript: {
files: '**/*.js',
tasks: ['jshint:file']
}
});
I use
node.js 0.8.21
grunt-cli 0.1.6
grunt 0.4.0rc7
grunt-contrib-jshint 0.2.0
grunt-regarde: 0.1.1

Resources