我们如何通过npm与Meteor一起使用节点模块? [英] How do we or can we use node modules via npm with Meteor?

查看:276
本文介绍了我们如何通过npm与Meteor一起使用节点模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何或可以通过npm使用 Meteor 来使用节点模块?

How do we or can we use node modules via npm with Meteor?

或者那些依赖于 API 的包装?

Or is that something that will be dependent on the packaging API?

或者推荐使用规定的方法吗?

Or is there a prescribed method that is recommended?

推荐答案


2016年3月28日发布的 Meteor 1.3 提供了应用程序完整的ES6(ES2015)模块支持和开箱即用的NPM支持。应用程序和软件包现在可以直接在客户端和服务器上轻松加载NPM模块。

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.

如果可以使用1.3,那么请检查 http://guide.meteor.com/using-packages.html#installing- npm

If you can use 1.3, then check http://guide.meteor.com/using-packages.html#installing-npm.

例如,要使用moment.js:

For example, to use moment.js:

meteor npm install --save moment

然后在你的代码中:

import moment from 'moment';

// this is equivalent to the standard node require:
const moment = require('moment');

如果你需要使用旧版本的Meteor,请阅读下面的其余答案。

If you need to use an older version of Meteor, read the rest of the answer below.

Pre-Meteor 1.3

自v0.6.0起,Meteor在第三方软件包的帮助下直接与NPM模块集成。例如,要使用 ws 等模块,

Since v0.6.0, Meteor integrates directly with NPM modules with the help of a 3rd party package. For example, to use a module like ws,


  1. 运行 sudo npm install -g ws (或本地安装,请参阅

  2. 在您的服务器JavaScript文件中,

  1. Run sudo npm install -g ws (or for local installs, see this)
  2. In your sever JavaScript file,

var Websocket = Npm.require('ws');
var myws = new Websocket('url');


要使用核心节点模块,只需制作相应的 Npm.require()调用,例如 var Readable = Npm.require('stream')。可读

To use a core Node module, just make the corresponding Npm.require() call, e.g. var Readable = Npm.require('stream').Readable.

由于 230,000个NPM模块中的任何一个=http://meteorhacks.com/complete-npm-integration-for-meteor.html\"rel =nofollow noreferrer> NPM包由Arunoda开发。

You can use any of the more than 230,000 NPM modules directly with Meteor thanks to the NPM package developed by Arunoda.

您还可以从智能包中定义Npm包的依赖关系 - 来自 npm支持的初始公告

You can also define dependencies on Npm packages from smart packages - from the initial announcement of npm support:

您的智能包现在可以通过添加对 Npm.depends的调用直接定义依赖项 in package.js

Your smart package can now define dependencies directly, by adding a call to Npm.depends in package.js:

Npm.depends({
  "awssum": "0.12.2",
  "underscore.string": "2.3.1"
});

所有这些都适用于热代码重新加载,就像Meteor的其余部分一样。当您进行更改时,捆绑包将自动下载缺少的npm包并重新锁定其依赖项。

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.

要在服务器代码中使用NPM模块,请使用 Npm.require 因为您通常使用普通的 require 。值得注意的是, __ meteor_bootstrap __。require 已被删除,其所有用途已转换为 Npm.require

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与Meteor一起使用节点模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆