9

Less is amazing and I want to use to node.js because using less.js is not a good performance. I testing purpos i'm using xamp on windows and I install node.js but where and what i should write.. I install express.js npm install -g express and less npm install -g less

2 Answers 2

23

If you're using expressjs you can install

npm install less-middleware

and then in your application (app.js)

var lessMiddleware = require('less-middleware');

then you have to tell expressjs to use less-middleware by doing

app.configure(function(){
  //other configuration here...
  app.use(lessMiddleware({
    src      : __dirname + "/public",
    compress : true
  }));
  app.use(express.static(__dirname + '/public'));
});

now in your [appname]/public/stylesheets/custom.less

gets translated into regular css custom.css

Sign up to request clarification or add additional context in comments.

7 Comments

I thought I had to compile .less using cli each time I was testing... until I saw this answer. Thanks for introducing the middleware!
It's almost TOO EASY to use, this way. :)
In case someone else has the same problem I did: make sure you're referencing the .css file in your layout and not the .less file. If you reference the css file it will automagically be compiled for you (even if it doesn't exist on disk yet).
Better to define your dependency in package.json: "less-middleware": "latest" then npm install to resolve dependencies.
@gravadlax "npm install less-middleware --save" is the correct way to do it. It adds "less-middleware" to the package.json file for you, with the current. "less-middleware" : "latest" will not work well on a team, as whenever someone npm installs, if a new version is released, that team member will have different versions of the dependency.
|
8

If you're using express 4.x and less-middleware 0.2.x beta (which is the latest at the moment), the syntax is a bit different.

This is the same:

$ npm install less-middleware

But the middleware has a source and three option parameters:

function(source, options, parserOptions, compilerOptions)

Example:

app.use(require('less-middleware')(
    __dirname + 'public/style/less', // source
    { dest: __dirname + 'public/style/css' }, // options
    {}, // parser
    { compress: 'auto' } // complier
));

app.use(express.static(__dirname + '/public'));

The complier's auto compress is really nice, style.css will result in an uncompressed and style.min.css will give you a compressed file.

For more info you should check out the Migration guide and the source code here: https://github.com/emberfeather/less.js-middleware

1 Comment

Cheers for that, only just realised the accepted answer is 2 years outdated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.