javascript 我们如何或可以通过 npm 和 Meteor 使用节点模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10165978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do we or can we use node modules via npm with Meteor?
提问by Steeve Cannon
采纳答案by Dan Dascalescu
Meteor 1.3, released on March 28, 2016, gives apps full ES6 (ES2015) modules support and out of the box NPM support. Apps and packages can now load NPM modules directly and easily on the client and on the server.
Meteor 1.3于 2016 年 3 月 28 日发布,为应用程序提供完整的 ES6 (ES2015) 模块支持和开箱即用的 NPM 支持。应用程序和包现在可以直接轻松地在客户端和服务器上加载 NPM 模块。
If you can use 1.3, then check http://guide.meteor.com/using-packages.html#installing-npm.
如果您可以使用 1.3,请检查http://guide.meteor.com/using-packages.html#installing-npm。
For example, to use moment.js:
例如,使用moment.js:
meteor npm install --save moment
Then in your code:
然后在你的代码中:
import moment from 'moment';
// this is equivalent to the standard node require:
const moment = require('moment');
If you need to use an older version of Meteor, read the rest of the answer below.
如果您需要使用旧版本的 Meteor,请阅读下面的其余答案。
Pre-Meteor 1.3:
Pre-Meteor 1.3:
Since v0.6.0, Meteor integrates directly with NPM moduleswith the help of a 3rd party package. For example, to use a module like ws
,
从 v0.6.0 开始,Meteor在 3rd 方包的帮助下直接与 NPM 模块集成。例如,要使用像这样的模块ws
,
- Run
sudo npm install -g ws
(or for local installs, see this) In your sever JavaScript file,
var Websocket = Npm.require('ws'); var myws = new Websocket('url');
- 运行
sudo npm install -g ws
(或对于本地安装,请参阅此) 在您的服务器 JavaScript 文件中,
var Websocket = Npm.require('ws'); var myws = new Websocket('url');
To use a core Node module, just make the corresponding Npm.require()
call, e.g. var Readable = Npm.require('stream').Readable
.
要使用核心节点模块,只需进行相应的Npm.require()
调用,例如var Readable = Npm.require('stream').Readable
.
You can use any of the more than 230,000 NPM modulesdirectly with Meteor thanks to the NPM packagedeveloped by Arunoda.
借助 Arnoda 开发的NPM 包,您可以将超过230,000 个 NPM 模块中的任何一个直接与 Meteor一起使用。
You can also define dependencies on Npm packages from smart packages - from the initial announcementof npm support:
您还可以从智能包中定义对 Npm 包的依赖——从npm 支持的最初声明开始:
Your smart package can now define dependencies directly, by adding a call to Npm.depends
in package.js
:
您的智能包现在可以通过添加对Npm.depends
in的调用来直接定义依赖项package.js
:
Npm.depends({
"awssum": "0.12.2",
"underscore.string": "2.3.1"
});
All of this works well with hot code reload, just like the rest of Meteor. When you make changes, the bundler will automatically download missing npm packages and re-pin its dependencies.
所有这些都适用于热代码重新加载,就像 Meteor 的其余部分一样。当您进行更改时,捆绑程序将自动下载缺少的 npm 包并重新固定其依赖项。
To use an NPM module within server code, use Npm.require
as you would normally use plain require
. Notably, __meteor_bootstrap__.require
has been eliminated and all of its uses have been converted to Npm.require
.
要在服务器代码中使用 NPM 模块,请Npm.require
像通常使用普通require
. 值得注意的是,__meteor_bootstrap__.require
已被淘汰,其所有用途均已转换为Npm.require
.
There is a small example of using an NPM module in your application.
回答by Raynos
Note that this answer applies to versions of Meteor prior to 0.6.0, which was released in April 2013 and added direct
npm
integration
请注意,此答案适用于 0.6.0 之前的 Meteor 版本,该版本于 2013 年 4 月发布并添加了直接
npm
集成
Install modules as you normally would through npm
and then use
像往常一样安装模块npm
,然后使用
var require = __meteor_bootstrap__.require,
pd = require("pd"),
after = require("after") // etc
Load any modules you want
加载任何你想要的模块
回答by gadicc
I did a complete write-up on this on Meteorpedia:
我在 Meteorpedia 上写了一篇完整的文章:
http://www.meteorpedia.com/read/npm
http://www.meteorpedia.com/read/npm
The article covers how to use npm in both your app and/or packages, and common patterns for wrapping regular callbacks and event emmitter callbacks to work properly in Meteor and Fibers, and include's references to Arunoda's async-utilities and additional resources.
本文介绍了如何在您的应用程序和/或包中使用 npm,以及包装常规回调和事件发射器回调以在 Meteor 和 Fibers 中正常工作的常见模式,并包括对 Arunoda 的异步实用程序和其他资源的引用。
回答by Akshat
You could use the Meteor Npmpackage
您可以使用Meteor Npm包
meteor add meteorhacks:npm
Then create a packages.json
file in your project's root directory with the NPM module's info.
然后packages.json
在项目的根目录中创建一个包含 NPM 模块信息的文件。
{
"redis": "0.8.2",
"github": "0.1.8"
}
Then as simple as (server side)
然后就像(服务器端)一样简单
var github = Meteor.npmRequire("github");
var redis = Meteor.npmRequire("redis");
So you just use Meteor.npmRequire
instead of require
所以你只需使用Meteor.npmRequire
代替require
回答by kenyee
I wrote a Gist on how to do this as of Meteor 0.6.5, How to add Node.js npms to your Meteor.js project.
我写了一篇关于如何从 Meteor 0.6.5 开始执行此操作的要点,如何将 Node.js npms 添加到您的 Meteor.js 项目。
回答by Mitar
I am using such a scriptwhich nicely install all Node.jsdependencies. It behaves similar to the official support in the Meteor engine branch (it installs dependencies at runtime), but it also supports installing from Gitrepositories and similar goodies.
我正在使用这样一个脚本,它可以很好地安装所有Node.js依赖项。它的行为类似于 Meteor 引擎分支中的官方支持(它在运行时安装依赖项),但它还支持从Git存储库和类似的好东西安装。