[英]Npm.require can't find node module
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. 我一直在尝试使用
Npm.require
来获取ldapjs
模块并将其用于客户端身份验证,但是我收到了以下消息。
var ldap = Npm.require('ldapjs');
Error: Cannot find module 'ldapjs'
Isn't the require
supposed to download the package from the npm? require
不是应该从npm下载软件包吗?
Currently the best way to use NPM packages in Meteor is this npm smart package . 当前在Meteor中使用NPM软件包的最佳方法是此npm智能软件包 。 The instructions on how to use it are pretty clear there.
那里的使用说明非常清楚。 Basically, you do three things:
基本上,您要做三件事:
1) Install npm: 1)安装npm:
mrt add npm
2) Create packages.json
file with list of required packages: 2)创建带有所需软件包列表的
packages.json
文件:
{
"ldapjs": "0.6.3"
}
3) Use the package via Meteor.require: 3)通过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. Meteor仅会使用
Npm.depends({...})
指令下载只要在智能包中声明的节点模块。 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: 在Meteor软件包中使用npm模块需要两件事:
Note that you need to write a package to use an npm module. 请注意,您需要编写一个软件包才能使用npm模块。 You'll probably want to read through the Meteor docs on packages .
您可能需要通读包上的Meteor文档。
For an example, check out the logging package in Meteor. 例如,请查看Meteor中的日志记录包。 Its package.js specifies a depency on the npm module cli-color, and its logging.js file requires and uses the module.
它的package.js在npm模块cli-color上指定了依赖关系,它的logging.js文件需要并使用该模块。
What if you want to use npm from the main application? 如果要从主应用程序使用npm怎么办? 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. 1.在Meteor应用程序外部创建目录
npm-shim
。 We will use it in step 3. 我们将在步骤3中使用它。
2. Add these two files to it: 2.将这两个文件添加到其中:
File package.js
文件
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
文件
npm.js
NpmShim = {};
NpmShim.colors = Npm.require('colors');
// Add more modules as needed.
3. Edit smart.json and add this line: 3.编辑smart.json并添加以下行:
"npm-shim": { "path": <path to the directory created in step 1> },
4. Execute mrt update
then meteor add npm-shim
. 4.执行
mrt update
然后meteor add npm-shim
。
Node modules can be used from the Meteor main application without the need to manually install them. 可以从Meteor主应用程序使用节点模块,而无需手动安装它们。 Use
NpmShim.colors
instead of Npm.require('colors')
. 使用
NpmShim.colors
而不是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
). 如果需要更多模块,则必须将它们添加到
package.js
和npm.js
(请参阅注释// Add more modules as needed
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.