I have been trying to use the Npm.require
to get the ldapjs
module and use it for client authentication, however I am getting the following message.
var ldap = Npm.require('ldapjs');
Error: Cannot find module 'ldapjs'
Isn't the require
supposed to download the package from the npm?
Currently the best way to use NPM packages in Meteor is this npm smart package . The instructions on how to use it are pretty clear there. Basically, you do three things:
1) Install npm:
mrt add npm
2) Create packages.json
file with list of required packages:
{
"ldapjs": "0.6.3"
}
3) Use the package via Meteor.require:
var ldapjs = Meteor.require('ldapjs');
Nope, it is not. Meteor will only download a node module as long as it is declared within a smart package, with Npm.depends({...})
directive. If your code is not a part of some smart package, then you'll need to install the node module manually.
You need two things to use npm modules in Meteor packages:
Note that you need to write a package to use an npm module. You'll probably want to read through the Meteor docs on packages .
For an example, check out the logging package in Meteor. Its package.js specifies a depency on the npm module cli-color, and its logging.js file requires and uses the module.
What if you want to use npm from the main application? And what if you don't want to install node modules manually (a maintenance headache)?
This is possible with a workaround. Create a shim smart package to provide the node modules for the main application. Export the modules to the main application.
1. Create a directory npm-shim
outside your Meteor application. We will use it in step 3.
2. Add these two files to it:
File package.js
// npm dependencies are only available for packages. If you have npm
// dependencies for the main application, you need this workaround:
// Create a shim package to provide the npm dependencies to the main app.
Package.describe({
summary: "Meteor main application npm dependencies"
});
Npm.depends({
colors: '0.6.2',
// Add more modules as needed.
});
Package.on_use(function(api) {
api.export("NpmShim"); // Omit this for versions before 0.6.5
api.add_files("npm.js", "server");
});
File npm.js
NpmShim = {};
NpmShim.colors = Npm.require('colors');
// Add more modules as needed.
3. Edit smart.json and add this line:
"npm-shim": { "path": <path to the directory created in step 1> },
4. Execute mrt update
then meteor add npm-shim
.
Node modules can be used from the Meteor main application without the need to manually install them. Use NpmShim.colors
instead of Npm.require('colors')
.
If you need more modules, you have to add them to package.js
and npm.js
(see comment // Add more modules as needed
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.