简体   繁体   English

Npm.require找不到节点模块

[英]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模块需要两件事:

  1. Npm.depends - specify the modules you want to use, with versions. Npm.depends-指定要使用的模块以及版本。 Meteor's build system will download the package and manage its dependencies 流星的构建系统将下载软件包并管理其依赖项
  2. Npm.require - pull in a module, making it available in the current scope Npm.require-引入模块,使其在当前作用域中可用

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文件需要并使用该模块。

node modules in the main application 主应用程序中的节点模块

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. 将模块导出到主应用程序。

The exact steps 确切步骤

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

The result of this workaround 解决方法的结果

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.jsnpm.js (请参阅注释// Add more modules as needed )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM