Tim Drake
Tim Drake

Reputation: 75

Including Javascript Library in Node Express Routes

I'm trying to use the Sanford encryption library(sjcl) in my Express App. I've tried to the following in my app.js file:

var sjcl = require('.lib/sjcl.js');

Next I try to call sjcl.encrypt in my routes/journal.js file, but get an error that it's not defined.

Next I tried requiring the the library in my journal.js file at the beginning, but get the module ./lib/sjcl.js cannot be found.

The sjcl.js library does export the sjcl object so that doesn't seem to be it.

Any ideas on how I can gain access to the sjcl library from within my routers file?

Upvotes: 0

Views: 590

Answers (1)

SLaks
SLaks

Reputation: 887275

Next I try to call sjcl.encrypt in my routes/journal.js file, but get an error that it's not defined.

require() just returns an object representing that module.
var x = require(...) assigns that object to a local variable.
It doesn't affect other .js files.

Next I tried requiring the the library in my journal.js file at the beginning, but get the module ./lib/sjcl.js cannot be found.

That would happen if your relative path is wrong.

Upvotes: 1

Related Questions