简体   繁体   English

2013年流星NPM包

[英]2013 Meteor NPM Packages

Update this solution describes how to effectively use the new Npm system in Meteor. 更新 此解决方案描述了如何在Meteor中有效使用新的Npm系统。


What is the current method of using NPM packages in Meteor? 目前在Meteor中使用NPM包的方法是什么?

As of March 22, 2013, there is no official documentation on this. 截至2013年3月22日,没有关于此的官方文档。

There are several questions about this, notably this one , however the solution seems outdated: the engine branch no longer exists, and I haven't been able to find anything on Npm.require in Meteor. 关于这一点有几个问题,特别是这个问题,但解决方案似乎已经过时了:引擎分支已不复存在,而且我无法在Meteor的Npm.require上找到任何内容。

Another solution, posted here , instructs to install into the .meteor/ build folders. 此处发布的另一个解决方案是指示安装到.meteor/ build文件夹中。 As I am installing to Heroku, this doesn't seem like a valid solution, as the buildpack uses meteor bundle to bundle the program before running it. 当我安装到Heroku时,这似乎不是一个有效的解决方案,因为buildpack使用meteor bundle捆绑程序,然后再运行它。 Thus, the temporary build folders don't seem like a valid option. 因此,临时构建文件夹似乎不是一个有效的选项。

What has happened to Npm in meteor? 流星中的Npm发生了什么? What's the latest way of using Npm packages? 使用Npm包的最新方法是什么?

On a related note, I'm trying to use the Amazon SDK (for s3) - would it be better to just package it as a Meteorite package instead? 在相关的说明中,我正在尝试使用亚马逊SDK(针对s3) - 将它打包为陨石包更好吗?

Arunoda has created an NPM Atmosphere package that allows you to use any NPM module like you're used to. Arunoda 创建了一个NPM Atmosphere软件包 ,允许您像以前一样使用任何NPM模块。 It's very simple. 这很简单。

First, mrt add npm . 首先, mrt add npm

You can also install the package by using meteor-npm command from npm install -g meteor-npm . 您也可以使用npm install -g meteor-npm meteor-npm命令安装软件包。

Next, make a packages.json file in your root project directory, with the package names and versions: 接下来,使用包名称和版本在根项目目录中创建packages.json文件:

{
    "foobar": "0.3.5",
    "loremipsum": "2.1.4"
}

Finally, use them with Meteor.require , like this: var FooBar = Meteor.require('foobar'); 最后,将它们与Meteor.require一起使用,如下所示: var FooBar = Meteor.require('foobar');

The current way of using NPMs in Meteor 目前在流星中使用NPM的方法

  1. Replace the x's below with the NPM name 用NPM名称替换下面的x
  2. Place the files outline below in /meteor-project-root/packages/x/ 将下面的文件大纲放在/ meteor-project-root / packages / x /中
  3. meteor add x 流星添加x
  4. To use it just call X in your code (X.function()) 要使用它,只需在代码中调用X(X.function())

x.js -------- x.js --------

X = Npm.require('x');

package.js -------- package.js --------

Package.describe({
  summary: "Meteor smart package for x node.js package"
});

Npm.depends({
  "x": "0.1.1"
});

Package.on_use(function (api) {
  api.add_files("x.js", ["client", "server"]);
});

Note: some packages will only work on client or server, if you are having issues, try only include the side you are going to use it on. 注意:某些软件包只能在客户端或服务器上运行,如果遇到问题,请尝试仅包含您要使用它的一方。

I have been using the fantastic " browserify ", which works like a charm. 我一直在使用梦幻般的“ browserify ”,它就像一个魅力。 This is an alternative to using Arunda's NPM Atmosphere package , or using Npm.require with package.js , that arguably has some advantages: 这是使用Arunda 的NPM Atmosphere包 ,或者使用带有package.js的Npm.require的替代方案,可以说有一些优点:

  1. My code can use plain old "require" instead of Npm.require or Meteor.require. 我的代码可以使用普通的旧“require”而不是Npm.require或Meteor.require。 Obviously this is not a huge deal, but if I want to use this code outside Meteor it's nice to feel it's not dependent on Meteor. 显然这不是一个大问题,但如果我想在Meteor之外使用这个代码,那么感觉它不依赖于Meteor是很好的。
  2. I don't have to worry about whether Meteor will once again change the way it thinks about Npm integration again. 我不必担心Meteor是否会再次改变它对Npm集成的看法。
  3. It allows me to use local development version of my own npm modules using npm link. 它允许我使用npm链接使用我自己的npm模块的本地开发版本。

Here's how it works: 以下是它的工作原理:

  1. I create a separate project for npm dependencies in a hidden .npm folder 我在隐藏的.npm文件夹中为npm依赖项创建了一个单独的项目
  2. I use browserify to create a bundle.js that will be loaded by meteor 我使用browserify创建一个将由meteor加载的bundle.js
  3. I use grunt watch to make sure that every time I install a new npm package, the bundle.js is updated 我使用grunt watch来确保每次安装新的npm包时,都会更新bundle.js

Here's my directory structure: 这是我的目录结构:

my_meteor_project/
    lib/
        bundle.js

    .npm/
        node_modules
        README.md
        Gruntfile.js
        entrypoint.js
        package.json

Here's an example of entrypoint.js (unfortunately I have to use globals so that assert, url, and _ are available in Meteor code) 这是一个entrypoint.js的例子(遗憾的是我必须使用全局变量,以便断言,url和_在Meteor代码中可用)

assert = require('assert');
url = require("url");
_ = require('underscore');

Here's the gruntfile: 这是gruntfile:

module.exports = function(grunt) {
  grunt.initConfig({
    watch: {
      build: {
          files: ['./entrypoint.js', './package.json'],
          tasks: ['browserify2'],
          options: {
          }
      }
    },
    browserify2: {
      compile: {
        entry: './entrypoint.js',
        compile: '../lib/bundle.js'
      }
    },
  });

  grunt.loadNpmTasks('grunt-browserify2');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('build', ['browserify2']);
};

I then use grunt watch to watch for changes to entry.js or new NPM installs 然后我使用grunt watch来监视entry.js或新NP​​M安装的更改

$ cd .npm
$ grunt watch:build &
[2] 44617
$ Running "watch:build" (watch) task
Waiting...

And then if I install an npm module, or modify entrypoint.js, bundle.js is updated: 然后如果我安装了一个npm模块,或者修改了entrypoint.js,就会更新bundle.js:

$ npm install url -save
npm http GET https://registry.npmjs.org/punycode
npm http GET https://registry.npmjs.org/querystring
npm http 304 https://registry.npmjs.org/punycode
npm http 304 https://registry.npmjs.org/querystring
url@0.7.9 node_modules/url
├── querystring@0.1.0
└── punycode@1.0.0
$ OK
>> File "package.json" changed.

Running "browserify2:compile" (browserify2) task
File written to: ../lib/bundle.js

Done, without errors.
Completed in 1.256s at Thu Jul 11 2013 11:36:22 GMT-0600 (MDT) - Waiting...

You can use https://atmospherejs.com/meteorhacks/npm 您可以使用https://atmospherejs.com/meteorhacks/npm

meteor add meteorhacks:npm

And then you can setup your package.json file: 然后你可以设置你的package.json文件:

{
  "redis": "0.8.2",
  "github": "0.1.8"
}

And use these packages: 并使用这些包:

var GithubApi = Meteor.npmRequire('github');

as you are using meteorite, when you install a node module to .meteor/local/build/server/ you actually install to 当您使用陨石时,将节点模块安装到.meteor/local/build/server/实际安装到

~/.meteorite/meteors/meteor/meteor/f07715dc70de16a7aab84e56ab0c6cbd9c1f9dc6/dev_bundle/lib/node_modules

when you use mrt bundle to create your deployment bundle, the additional packages get bundled as well. 当您使用mrt bundle创建部署包时,其他包也会捆绑在一起。

I have not tried it on Heroku but I checked that the node module gets packaged when using mrt bundle. 我没有在Heroku上尝试过,但我检查了节点模块在使用mrt bundle时被打包了。

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

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