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

Unable to load Axios in Node even though it is installed

我有一个 Jasmine 测试,其中包含一些 HTTP 请求。我将 Axios 用于基于 promise 的 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));
            });
    });
});

这是输出:

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)

这里是 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"
  }
}

这是怎么回事?

您已定义:

var request = require('axios');

并尝试调用:

axios.get ...

改为:

var axios = require('axios');