Tag: 模块

NodeJS导出模块

浏览NPM存储库中的现有模块,我已经看到使用以下模块: module.exports = (function(){ return { // objects from the module }; })(); 和使用的模块 module.exports = { //objects }; 跟他们有什么不同吗? Node中是否有模块组织的第三个选项/模式? 谢谢!

一般术语放在上下文中

在学习任何其他编程语言之前,我学习了Java编程。 当我学习Node.js时,我得到了所有的术语混淆。 我一直认为API是一个方法库,类,等,有人build立了使我们的生活更容易。 然后我了解了模块,我基本上认为它和API(已经由某人build立的方法列表)是一样的。 那么,我了解了Express框架,它又是一个像模块和API一样的方法列表。 而且,我们将这些function整合到我们的程序中的方式都是通过类似的方式进行的 Var http = require('http'); 因此,理解这些术语之间区别的人是否可以将这些术语放在可以解决我的问题的上下文(示例)中。 非常感谢您的帮助。

build立NodeJs模块(或任何其他库)

我想用C ++和Ubuntu 13.04构build一个nodejs模块,使用如下所示的一些boost头文件: #include <iostream> #include <string> #include <node/node.h> #include <v8.h> #include <boost/lexical_cast.hpp> using namespace std; using namespace v8; Handle<Value> Method(const Arguments& args) { HandleScope scope; std::string foobar = "8"; return scope.Close(String::New("world")); } void init(Handle<Object> exports){ exports->Set(String::NewSymbol("Hello"), FunctionTemplate::New(Method)->GetFunction()); } NODE_MODULE(hello, init) 但是,使用node-gyp进行编译时,出现以下错误: sam @ ubuntu:〜/ workspace_cpp / NodeTest / src $ node-gyp build gyp info如果以ok结束,它就会工作gyp […]

Grunt以“无法find模块”为本地模块失败

我是node.js和grunt的新手。 我search了“无法find模块”的问题,但是我遇到的模块是本地的,所以这可能是特定于站点的。 我已经inheritance了一个已知有效的Gruntfile和一个已知有效的packages.json文件的存储库,即使我已经安装了grunt并安装了所有的依赖关系,但运行grunt时我仍然遇到以下故障: Error: Cannot find module './grunt/util' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) … at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) 请注意,这并不涉及grunt-util模块。 这是一个名为“util”的本地模块,所以安装grunt-util并不能解决这个问题。 有没有人跑过类似的东西?

如何为具有构造函数的复杂外部commonjs模块(如imap)编写TypeScript声明文件?

这个问题是我早期的一个改进: 如何为具有构造函数的外部commonjs模块编写TypeScript声明文件? 我试图为imap模块编写一个声明文件: – imap on npm – node-imap这个文件应该是什么样的? 在没有find我上面提到的原始问题的满意答案之后,我意识到这个语言有一些相互竞争的特征: 任何导出或导入都会使模块成为外部模块 似乎没有办法从具有(模块级)构造函数的外部模块导出多个实体,因此共享实体必须位于模块外(环境)。 看我以前的问题(上面)。 没有办法将外部模块导入到声明模块之外的声明文件中 下面的模式是不允许的: import fs = require('fs'); declare module 'A' {} import fs = require('fs'); declare module 'A' {} 这是我到目前为止的声明文件: interface IIMAPAccount { user: string; password: string; host: string; port: number; tls?: boolean; } interface IEMail { mail: any; } interface ICriteria { […]

node.js在mongodb驱动程序文档中声明模块

这是mongoDB驱动程序文档的一个例子。 我一直在试图找出assert.equal在这个例子中做了些什么,但是在Node.js网站上的官方文档并没有太多的帮助 – 官方文档中写着:“testing浅,与平等比较运算符强制平等(==)“。 所以我首先怀疑它会根据平等的真值返回真假。 但似乎没有。 我看了这个post: 断言模块在nodejs中使用? 。 它确实有帮助 – 但还不够。 我还是不太了解“unit testing”是如何完成的。 任何帮助将不胜感激,但坚实的例子将是超级有益的! var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); // Set up the connection to […]

什么是ES6中的子模块?

假设我有一个名为a.js的ES6模块。 a.js模块导入b.js和c.js模块。 同样, b.js导入d.js和c.js导入e.js 现在我想知道哪些是子模块和超级模块。 我觉得b.js和c.js是c.js的子模块。 但我不确定。 真的很困惑。 请帮忙。 提前致谢。

具有NodeJS / CommonJS风格模块系统的语言

我真的很喜欢NodeJS(和它的浏览器端对应)处理模块的方式: var $ = require('jquery'); var config = require('./config.json'); module.exports = function(){}; module.exports = {…} 实际上我对ES2015的“导入”规范感到非常失望,这与大多数语言非常相似。 出于好奇,我决定寻找其他语言来实现甚至支持类似的出口/import风格,但无济于事。 也许我错过了一些东西,或者更有可能的是,我的Google Foo没有达到要求,但是看看其他语言以类似的方式工作真的很有趣。 有没有人遇到类似的系统? 或者,甚至有人甚至可以提供不经常使用的原因。

Nodejs Amazon S3file upload

我试图创build一个表单上传我的本地文件到我的S3桶,但我有点困惑,上传逻辑应该存在的部分。 我想保持我的POST路线干净,并引用在一个单独的文件中的AWS逻辑,但我有点困惑,什么值应该用于参数级别的身体属性,以及我应该如何引用这个模块在fileAttachment行设置完成的上传URL到我的数据库属性。 任何人都可以指向正确的方向吗? 这是我的aws-s3.js文件: module.exports = function() { var path = require('path'); var async = require('async'); var fs = require('fs'); var AWS = require('aws-sdk'); var config = require(path.resolve(__dirname, '..', '..','./config/config.js')); AWS.config.region = config.region; var s3 = new AWS.S3({params: {Bucket: config.awsBucket}}); var params = {Key: config.awsAccessKeyId, Body: req.body.fileAttachment}; s3.upload(params, function(err, data){ if (err) { console.log("Error uploading […]

为什么react.js不认识自己的node.js模块?

我正在尝试为react.js做一个hello world,然而react.js在第3行失败。 该错误是一个参考错误: ReferenceError: Can't find variable: module react.js:3 我认为这是一个节点模块,节点恰好是我所服务的html 。 不完全确定为什么这是…有人打赌下赌注或线索我? 代码非常简单: main.js // main.js var React = require('react'); var ReactDOM = require('react-dom'); ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('example'); ); 的index.html <!DOCTYPE html> <html> <head> <title>Hello React!</title> <script src="node_modules/react/react.js"></script> <script src="node_modules/react-dom/dist/react-dom.js"></script> <script src="main.js"></script> </head> <body> <div id="example"></div> </body> </html>