Setting up sails.js: How to make the node module accessible through the sails project (controller / model, etc.)?

I am just starting out with SailsJS as my first Node web framework. Say I want to add MomentJS and use it in an application. How to configure it?

+6
source share
3 answers

you can use bootstrap.js (in config /)

as:

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

in all sails files you can use

 sails.moment() 

Now.

+10
source

SailsJS is no different from any other NodeJS application. So, on top of your (say) Controller.js file, you do

 var m = require("moment"); 

And you are good to go. The @mdunisch method will obviously allow you to use the moment package in your application, without the need to “require” in each file.

+2
source

If you are trying to include your node_modules in the client side, for example, jQuery, AngularJS or one of many many font libraries, you can npm install them as usual, but just to be sure of the sails you are editing your tasks/config/copy.js and add a new block, for example:

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

LESS can be @import ed, as usual, without copying. Other assets will need to be copied as described above. If you use the sails linker , be sure to also add your JS paths to tasks/pipeline.js (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 is not clear how to synchronize npm modules with directories available on the Internet.

+2
source

Source: https://habr.com/ru/post/969936/


All Articles