我正在尝试与网关交互并想尝试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 错误错误:找不到模块“请求”。计算路径'/Users/admin/Library/Developer/CoreSimulator/Devices/ACA260DA-C0FD-4373-8822-4474222199751/data/Containers/Bundle/Application/EE654C43-B629-4817-9252F-appaG4-9252F-AG4EIntegration 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
我已经尝试重新安装打字稿和类型/请求模块和 node_modules 文件夹,以及其他版本的打字稿。安装该模块时我错过了什么吗?
你不能使用request
npm 模块,因为它是一个依赖于内置节点功能的 nodeJS 模块,比如net模块。
为了发出 http 请求,您可以使用跨平台模块的http
实现,如 NativeScript 文档文章中所示 - http://docs.nativescript.org/cookbook/http
如果您需要发出 https 请求,您可以使用 nativescript-https 插件 - https://www.npmjs.com/package/nativescript-https
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句