15

Is it possible to use Npm-Modules on client-side?

More specifically:

I want to use node.js built-in crypto-module for encrypting a password the user enters and then send the encrypted password with a signature(/hmac) to my server.

I need to do it that way, because I must be able to retrieve the original password server-side, because afterwards I'm going to send it to a ldap-server, because the user should authenticate with the same username/password he is registered with on that server.

This is what I did:

created in packages/crypto/: -package.js:

Package.on_use(function(api) { api.add_files('crypto.js',['server','client']);});

-crypto.js: crypto = Npm.require("crypto");

It works fine on the server, but on the client it says "Reference Error: Npm is not defined". So, is it possible to use the crypto-module on client-side?

Are there any alternatives for achieving this goal?

Thank you!

Edit: Is there any good alternative for getting the password to the server in a secure way, so that the server can retrieve the original password? I think doing the ldap()-request on the client-side (like: if(checkLdap(usrname,password)){<login>} else{fail}) can be easily bypassed?

Peter W
  • 351
  • 3
  • 9

4 Answers4

14

You can try to add the js-files you need on client-side from .npm folder under crypto's package directory. So, your package.js file might look like this:

Package.describe({
  summary: 'Description of your crypto package'
});

Npm.depends({
  'crypto': '1.0.0'
});

Package.on_use(function (api) {
  api.add_files('crypto.js', 'server');
  api.add_files('.npm/node_modules/crypto/crypto.js', 'client');
});
th0r
  • 515
  • 4
  • 9
  • This probably wont work as the contents of the client files get wrapped in a function call, so unless these packages explicitly add themselves to the global window object they wont be available. – Bjorn Jun 23 '13 at 14:03
  • This solution worked fine with every library I packaged so far because all of them (and imho 99% of others) explicitly add themselves to the global window object. – th0r Jun 23 '13 at 19:16
  • Thanks...was looking for a better way to do this, but this will do for now. – Andrew Mao Jun 24 '13 at 20:41
  • Ah, I only tried it with only one and that one didn't work. It's good to know that other libraries add themselves to the window object. Thanks. – Bjorn Jun 25 '13 at 15:18
  • 1
    To make it work with the current version you should change the path to `.npm/package/node_modules/crypto/crypto.js'` – Michiel Sep 10 '14 at 08:46
  • I've found this to be helpful as well. Many client side modules use the umd wrapper and will expose themselves on the window. Another alternative is to use bower, but that runs the risk of loading unnecessary files, so the npm approach is preferrable imho. – Nick Tomlin Dec 13 '14 at 05:01
5

You can use https://github.com/elidoran/cosmos-browserify now to archive this. I used wrapped packages before and it was real pain to update them and to create new ones. Now with browserify support I can include library with just several lines of code. See their example how to do it. I don't publish it here as it may be subject of change.

Igor Loskutov
  • 2,157
  • 2
  • 20
  • 33
  • I tried this, it was simple and it worked well. Recommended! Follow the instructions under the heading "Use in a Meteor App". – joeytwiddle Sep 30 '15 at 17:14
2

Its not possible to use Npm modules on the client side since Npm modules are extensions via node.js which only runs on the server end.

If you want to use a file like crypto you would have to make a client side only version and put it in /client/lib of your Meteor app

While this may be possible officially, Meteor doesn't support this.

You would have to include requirejs manually using this project: https://github.com/apendua/require

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • 2
    Many npm modules are designed to be usable on either the server or client, not just server. – protometa Oct 07 '14 at 02:35
  • @protometa it may be but I don't think Meteor supports this without additional third party stuff such as browserify. Officially npm, which meteor is dependent on, doesn't directly support client side modules so Meteor doesn't work with them client side. You can browserify them and manually include them like you would for any other nodejs project. – Tarang Oct 07 '14 at 05:15
  • @Akshat Correct, but Meteor typically supports easy sharing of server and client code. It's unusual to me that npm modules should be an exception. – protometa Oct 09 '14 at 17:17
  • Npm _does_ officially support client side packages. The tagline on their website (Well one of the several) reads "the package manager for browsers". – Nick Tomlin Dec 12 '14 at 00:08
  • @NickTomlin I don't dispute this, the easiest way to get it to work with meteor is to manually attach the file. Meteor's `npm` implementation does not support client side npm. – Tarang Dec 12 '14 at 06:16
  • It does now, this answer is outdated https://guide.meteor.com/using-npm-packages.html – Bart Louwers Jan 14 '18 at 13:16
1

You can use browserify to build a .js bundle with all all the Npm modules you want on the client side. See:

2013 Meteor NPM Packages

Community
  • 1
  • 1
Jonathan Warden
  • 2,492
  • 1
  • 17
  • 11