import request from 'request-promise-native';
報錯
request_promise_native_1.default is not a function
解決方法:引入方式改成
import * as request from 'request-promise-native';
打印引入的request 查看不一樣的引入方式 request 值有什麼區別javascript
----1-------------------------------------------
request-promise-native包的導出方式是:module.exports = request;
不一樣引入方式,打印結果: 1. import * as request from 'request-promise-native'; 打印request ,結果以下: function request(uri, options, callback) { if (typeof uri === 'undefined') { throw new Error('undefined is not a valid uri or options object.') } var params = initParams(uri, options, callback) if (params.method === 'HEAD' && paramsHaveRequestBody(params)) { throw new Error('HTTP HEAD requests MUST NOT include a request body.') } return new request.Request(params) } 2. import request from 'request-promise-native'; 打印 request 結果是 undefined
3. import {request} from 'request-promise-native';
打印 request 結果是 undefined
----2-------------------------------------------
若是 request-promise-native 包導出方式改成以下方式:
包導出: module.exports.request = request; 等同於 module.exports = {request}; 不一樣引入方式,打印結果: 1. import * as request from 'request-promise-native'; 打印request ,結果以下: { request:
{ [Function: request] get: [Function], head: [Function], options: [Function], post: [Function], put: [Function], patch: [Function], del: [Function], delete: [Function], jar: [Function], cookie: [Function], defaults: [Function], forever: [Function], Request: { [Function: Request] super_: [Object], debug: undefined, defaultProxyHeaderWhiteList: [Array], defaultProxyHeaderExclusiveList: [Array] }, initParams: [Function: initParams], debug: [Getter/Setter] }
}
2. import request from 'request-promise-native'; 打印 request 結果是 undefined 3. import {request} from 'request-promise-native'; 打印 request 結果以下: function request(uri, options, callback) { if (typeof uri === 'undefined') { throw new Error('undefined is not a valid uri or options object.') } var params = initParams(uri, options, callback) if (params.method === 'HEAD' && paramsHaveRequestBody(params)) { throw new Error('HTTP HEAD requests MUST NOT include a request body.') } return new request.Request(params) }
----3-------------------------------------------
若是 request-promise-native 包導出方式改成以下方式:
包導出: module.exports.default = request;
不一樣引入方式,打印結果: 1. import * as request from 'request-promise-native'; 打印request ,結果以下: { default: { [Function: request] get: [Function], head: [Function], options: [Function], post: [Function], put: [Function], patch: [Function], del: [Function], delete: [Function], jar: [Function], cookie: [Function], defaults: [Function], forever: [Function], Request: { [Function: Request] super_: [Object], debug: undefined, defaultProxyHeaderWhiteList: [Array], defaultProxyHeaderExclusiveList: [Array] }, initParams: [Function: initParams], debug: [Getter/Setter] }
} 2. import request from 'request-promise-native'; 打印 request 結果以下:
function request(uri, options, callback) {
if (typeof uri === 'undefined') {
throw new Error('undefined is not a valid uri or options object.')
}
var params = initParams(uri, options, callback)
if (params.method === 'HEAD' && paramsHaveRequestBody(params)) {
throw new Error('HTTP HEAD requests MUST NOT include a request body.')
}
return new request.Request(params)
}
3. import {request} from 'request-promise-native';
打印 request 結果是 undefined
-----總結----------------------------------------
導出:( = 右側的 request 是一個function,從源碼中得知)
out1: module.exports = request; // 導出的是 function
out2: module.exports.request = request; // 導出的是對象 {request:function}
out3: module.exports.default = request; // 導出的是對象 {default:function}
引入:
in1: import * as request from 'request-promise-native'; // * as request => out1: request = function; out2: request = {request:function}; out3: request = {default:function} => 導出的內容 = request
in2: import {request} from 'request-promise-native'; // request => out1: request = undefined; out2: request = function; out3: request = undefined => 解構賦值
in3: import request from 'request-promise-native'; // {request} => out1: request = undefined; out2: request = undefined; out3: request = function => module.exports.default = 隨意命名
查看不一樣引入方式的編譯結果:html
-----不一樣引入方式,打包編譯結果-----------------------java
request-promise-native 源碼:node
-------1-- module.exports = request -------------------------react
左側引入方式 import request from 'request-promise-native';es6
程序報錯: request_promise_native_1.default is not a functiontypescript
若是引入方式改成 :import {request} from 'request-promise-native';json
程序報錯: request_promise_native_1.request is not a functionpromise
-------2-----module.exports.default = request----------------------cookie
request-promise-native 源碼:最後一句導出 若改爲 module.exports.default = request;
-------3------module.exports = {request}---------------------
request-promise-native 源碼:最後一句導出 若改爲 module.exports = {request};
-----總結----------------------------------------
-------4---------------------------
request-promise-native 源碼:最後一句導出
可是程序仍報錯 SyntaxError: Unexpected token export
-----------------------------