简体   繁体   English

如何在node.js中要求插件模块?

[英]How to require plug-in module in node.js?

I'm testing a server (not done by me) using Mocha framework and SuperAgent module, however I need to test the server Ouath protocol. 我正在使用Mocha框架和SuperAgent模块测试服务器(不是我自己做的),但是我需要测试服务器Ouath协议。 I find the superagent-oauth module in order to make sign request. 我找到了superagent-oauth模块,以便发出签名请求。

However, when I require the module I get the following error 但是,当我需要该模块时,出现以下错误

TypeError: Object.keys called on non-object TypeError:在非对象上调用了Object.keys

My code, follows the readme in the superagent-oauth repo, and it is 我的代码遵循superagent-oauth存储库中的自述文件,它是

var superagent = require('superagent');
var log = require('./log.js');
var Oauth = require('node-oauth');
var oauth = new Oauth({..});

require('superagent-oauth')(superagent);

Then when running the test I got the TypeError: Object.keys called on non-object. 然后,在运行测试时,我得到了TypeError:在非对象上调用的Object.keys。

When using superagent I just do 当使用超级代理时,我只是做

var superagent = require('superagent');
agent = superagent.agent();

But I don't know how to use/require the superagent-oauth module. 但是我不知道如何使用/要求superagent-oauth模块。 So what's the way to do it correctly? 那么正确的方法是什么? Thank you 谢谢

Added the trace 添加了痕迹

2) Login 'Username:Password' test: POST with a valid login logins into the AM with a valid login: TypeError: Object.keys called on non-object at Function.keys (native) at Request.query (/home/maldo/testingAM/node_modules/superagent-oauth/superagent-oauth.js:22:23) at Context. 2)登录“ Username:Password”测试:使用有效登录名的POST使用有效登录名登录AM:TypeError:在Request.query(/ home / maldo)的Function.keys(本机)上的非对象上调用Object.keys /testingAM/node_modules/superagent-oauth/superagent-oauth.js:22:23)。 (/home/maldo/testingAM/loginUserPass.js:74:6) at Test.Runnable.run (/home/maldo/node_modules/mocha/lib/runnable.js:196:15) at Runner.runTest (/home/maldo/node_modules/mocha/lib/runner.js:343:10) at Runner.runTests.next (/home/maldo/node_modules/mocha/lib/runner.js:389:12) at next (/home/maldo/node_modules/mocha/lib/runner.js:269:14) at Runner.hooks (/home/maldo/node_modules/mocha/lib/runner.js:278:7) at next (/home/maldo/node_modules/mocha/lib/runner.js:226:23) at Runner.hook (/home/maldo/node_modules/mocha/lib/runner.js:246:5) at process.startup.processNextTick.process._tickCallback (node.js:244:9) (/home/maldo/testingAM/loginUserPass.js:74:6)在Test.Runnable.run(/home/maldo/node_modules/mocha/lib/runnable.js:196:15)在Runner.runTest(/ home /在Runner.runTests.next(/home/maldo/node_modules/mocha/lib/runner.js:389:12)的maldo / node_modules / mocha / lib / runner.js:343:10)在下一个(/ home / maldo / Runner.hooks(/home/maldo/node_modules/mocha/lib/runner.js:278:7)的下一节点(/ home / maldo / node_modules / mocha /)的node_modules / mocha / lib / runner.js:269:14)在Runner.hook(/home/maldo/node_modules/mocha/lib/runner.js:246:5)处的lib / runner.js:226:23)在process.startup.processNextTick.process._tickCallback(node.js:244)处:9)

I got this trace in the file loginUserPass a couple of times always in the .query() statament of the agent. 我总是在代理的.query()语句中多次在文件loginUserPass中获得此跟踪。 This happens when in the OauthAM.js I added superagent-ouath require. 当我在OauthAM.js中添加了superagent-ouath require时,就会发生这种情况。

Have you done npm install to retrieve all the module dependencies from npm? 您是否已完成npm install来从npm检索所有模块依赖项? It seems like the dependencies for superagent are not available perhaps? 似乎超级代理的依赖项可能不可用?

New dependencies are installed with (and put into package.json file) with npm install superagent --save . 新的依赖项通过npm install superagent --save安装(并放入package.json文件中)。

That way you'll get all the dependencies that the superagent module needs. 这样,您将获得superagent模块所需的所有依赖项。

Ran into same error message while installing vows on a new project created with npm init . 在使用npm init创建的新项目上安装誓言时遇到同样的错误消息。 This GitHub issues discussion suggested adding the following line to the package.json file for the project (ie the project you're trying to create, not the one you're trying to install): 这个GitHub问题讨论建议package.json下行添加到该项目(即您要创建的项目,而不是您要安装的项目)的package.json文件中:

 "dependencies": {}

This resolved it for me. 这为我解决了。 Of course, you mentioned you have dependencies installed in your package.json, so this answer is more for others who might search for the error message. 当然,您提到在package.json中安装了依赖项,因此对于可能搜索错误消息的其他人来说,此答案更多。

https://github.com/stephenplusplus/grunt-bower-install/issues/9 https://github.com/stephenplusplus/grunt-bower-install/issues/9

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

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