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

查看:20
本文介绍了我们如何或可以通过 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?

推荐答案

Meteor 1.3,于 2016 年 3 月 28 日发布,为应用程序提供了完整的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 在 3rd 方包的帮助下直接与 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(或对于本地安装,请参阅 this)
  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');

要使用核心 Node 模块,只需进行相应的 Npm.require() 调用,例如var Readable = Npm.require('stream').Readable.

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

您可以将任何超过 230,000 NPM 模块直接与 Meteor 一起使用,这要归功于 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:

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

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 模块,请像通常使用普通的 require 一样使用 Npm.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天全站免登陆