JS ERROR尝试执行简单的http请求时出错

时间:2017-08-02 08:27:11

标签: javascript typescript nativescript

我正在尝试与网关互动并想尝试https://www.npmjs.com/package/@types/request模块。

我正在尝试执行的代码:

export class OAuthAccessor {

    //some stuff

    public static createOAuthAccessToken() {
        this.makeCall();  //this line 11
    }

    private static makeCall() {
        var request = require('request');
        request.post({
            uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
            headers: {
                "Accept": "application/json",
                "Accept-Language": "en_US",
                "content-type": "application/x-www-form-urlencoded"
            },
            auth: {
                'user': this.clientID,
                'pass': this.secret,
                // 'sendImmediately': false
            },
            form: {
                "grant_type": "client_credentials"
            }
        }, function(error, response, body) {
            console.log("error = " + error);
            console.log(body);
        });
    }

}

错误:

  

file:///.../OAuthAccessor.js:11:30:JS ERROR错误:无法找到模块'请求'。 Computed path' /Users/admin/Library/Developer/CoreSimulator/Devices/ACA260DA-C0FD-4373-8822-447422199751/data/Containers/Bundle/Application/EE654C43-B629-4817-929F-F4E52DA1A99F/IPGIntegration.app /应用程序/ tns_modules /请求'

我的Package.json:

{
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "readme": "NativeScript Application",
  "repository": "<fill-your-repository-here>",
  "nativescript": {
    "id": "org.nativescript.IPGIntegration",
    "tns-ios": {
      "version": "3.1.0"
    },
    "tns-android": {
      "version": "3.1.1"
    }
  },
  "dependencies": {
    "@types/request": "^2.0.0",
    "nativescript-theme-core": "~1.0.2",
    "require": "^2.4.20",
    "tns-core-modules": "~3.1.0"
  },
  "devDependencies": {
    "babel-traverse": "6.25.0",
    "babel-types": "6.25.0",
    "babylon": "6.17.4",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "~0.5.0",
    "typescript": "~2.3.4"
  }
}

我的tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noEmitHelpers": true,
        "noEmitOnError": true,
        "lib": [
            "es6",
            "dom"
        ],
        "baseUrl": ".",
        "paths": {
            "*": [
                "./node_modules/tns-core-modules/*",
                "./node_modules/@types/request/*"
            ]
        }
    },
    "exclude": [
        "node_modules",
        "platforms",
        "**/*.aot.ts"
    ]
}

我做了什么: 使用打字稿模板

创建一个新项目
tns create my-app-name --template typescript
npm install typescript (version 2.3.4)
npm install @types/request

我已经尝试重新安装typescript和types / request模块以及node_modules文件夹,以及其他版本的typescript。我在安装该模块时是否遗漏了什么?

2 个答案:

答案 0 :(得分:1)

您不能使用request npm模块,因为它是一个nodeJS模块,具体取决于内置节点功能,如net模块。

为了发出http请求,您可以使用跨平台模块&#39; http实施,如NativeScript文档文章所示 - http://docs.nativescript.org/cookbook/http

如果您需要发出https请求,可以使用nativescript-https插件 - https://www.npmjs.com/package/nativescript-https

答案 1 :(得分:0)

也许尝试

require('@types/request')