Trying to Build Less (Less CSS) Using a Build Script with Nodejs

Trying to build LESS (less css) using a build script with nodeJS

Using node.js

var less = require('less')
,fs = require('fs');

fs.readFile('style.less',function(error,data){
data = data.toString();
less.render(data, function (e, css) {
fs.writeFile('style.css', css, function(err){
console.log('done');
});
});
});

Less to css via node script

The first works here, maybe You should

console.log(error)

in the readFile callback, most likely Your path is wrong and the file cant be opened.

Node.js + Express.js. How to RENDER less css?

Gosh, that stuff is very inconsistent in how the paths work, however I found out how you can get it to work.

The first problem is with your paths, both compiler and staticProvider, compiler needs to use /public and is will hook into all requests below that. If you don't do that, the compiler will try to use a path like /public/stylo/stylo.

The second problem lies with the @import in main.less file and the fact that less compiler is stupid and does not handle relative imports.

Using @import "/public/stylo/goodies.css"; in your main.less will make it work.

Filed a Bug for the relative path issue with less:

https://github.com/cloudhead/less.js/issues/issue/177

How to compile lesscss using node.js

You can install the LESS compiler directly from npm.

  1. Install node.js. Go to this page and download the installer for your platform.

    If you're on Windows, download the .msi; if you are on OSX, download the .pkg file. Whenever possible, download the 64-bit version (unless your system is running only 32-bit hardware and software). If you are on Linux and you want to use package managers, see this page.
  2. Once you have node.js installed, you should also have npm, which is node.js Package Manager. You can open a terminal/console and run npm -v to make sure everything is installed correctly.
  3. Eventually, you can install the LESS compiler by simply executing:

    npm install -g less

(note: on OSX and Linux you may need to run this with sudo: sudo npm install -g less).

The LESS compiler will then be available as the lessc command. See examples here.

PS: Some GUIs also exist for simplifying working with lessc. Google "less gui windows/mac/linux" to see many results, like this one for Mac.



Related Topics



Leave a reply



Submit