Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sails.js setup: How to make a node module available across the sails project (controller / model, etc)?

I just getting started with SailsJS as my first web framework on Node. Let's say I wanna add MomentJS in and use across the app. How to set it up?

like image 612
Dida Avatar asked May 28 '14 05:05

Dida


2 Answers

you can use the bootstrap.js (in config/)

like:

module.exports.bootstrap = function (cb) {
  sails.moment = require('moment');

cb();
};

in all Sails-Files you can use

sails.moment() 

now.

like image 133
mdunisch Avatar answered Nov 20 '22 15:11

mdunisch


If you're trying to include your node_modules into the client side, such as jQuery, AngularJS or one of the various many font libraries, then you can npm install them as normal, but just to be sure in sails you edit your tasks/config/copy.js and add a new block, example:

grunt.config.set('copy', {
    dev: {
        files: [{
            expand:true,
            cwd: './node_modules/font-awesome/fonts',
            src: ['**/*'],
            dest: '.tmp/public/fonts'
        }
    }
});

LESS can be @imported like normal without being copied around. Other assets will need to be copied as above. If you're using the sails linker then don't forget to add your JS paths to tasks/pipeline.js too (if necessary).

You can read more here: http://ash.zi.vc/sails/2016/02/02/including-client-side-node-modules-in-my-sails-application/

It's not directly obvious how to sync npm modules to the web accessible directories.

like image 4
zivc Avatar answered Nov 20 '22 15:11

zivc