即使安装了 Node,也无法在 Node 中加载 Axios

use*_*993 0 javascript node.js jasmine axios

我有一个 Jasmine 测试,其中包含一些 HTTP 请求。我正在使用 Axios 进行基于承诺的 HTTP 访问,但由于某种原因我得到了axios is not defined. 我已经跑了npm install axios --save

var request = require('axios');
var constants = require('../../lib/constants.js');

describe('Signing into the application', function () {
    it('returns 200 OK', function () {
        axios.get(constants.Endpoint)
            .then(function (response) {
                console.log(JSON.stringify(response));
            })
            .catch(function (error) {
                console.log(JSON.stringify(error));
            });
    });
});
Run Code Online (Sandbox Code Playgroud)

这是输出:

Failures:

  1) Signing into the application returns 200 OK
   Message:
     ReferenceError: axios is not defined
   Stacktrace:
     ReferenceError: axios is not defined
    at jasmine.Spec.<anonymous> (C:\Users\la\Documents\tests\spec\integration\sign-in\sign-in-spec.js:6:9)
Run Code Online (Sandbox Code Playgroud)

这里是package.json

{
  "name": "actual-tests",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jasmine-node spec"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.16.2",
    "jasmine-node": "^1.14.5"
  }
}
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

mjl*_*wky 5

您已定义:

var request = require('axios');
Run Code Online (Sandbox Code Playgroud)

并试图打电话:

axios.get ...
Run Code Online (Sandbox Code Playgroud)

将其更改为:

var axios = require('axios');
Run Code Online (Sandbox Code Playgroud)