request / request-promise-native

The simplified HTTP request client 'request' with Promise support. Powered by native ES6 promises.
ISC License
1.1k stars 63 forks source link

Can we add support of ES6 import / export modules? #1

Open cajoy opened 8 years ago

analog-nico commented 8 years ago

request-promise-native won't be implemented using ES6 modules because it still needs to support node v0.12. However, you can think of it as:

export default request

Would you like to import anything other than the main request function into your code?

Or is your question a more basic one? Then the answer would be: Yes, you can use babel for that.

cajoy commented 8 years ago

I just want to be consistent in my codebase.

I can easily do:

const request = require('request-promise-native');

but

import * as request from 'request-promise-native';

doesn't work :(

analog-nico commented 8 years ago

Unfortunately, I don't have a test set up for that so please give me more input:

  1. Do you use a transpiler? Babel?
  2. Does import request from 'request-promise-native' work?
  3. What error does import * as request from 'request-promise-native' produce?
  4. To what code is import * as request from 'request-promise-native' transpiled into?
cajoy commented 8 years ago

1) I use Typescript 2.0 2) It throws an error: error TS2307: Cannot find module 'request-promise-native'. 3) The same as 2) 4) It doesn't transpiles because of error in 2) and 3)

analog-nico commented 8 years ago

Alright, thanks.

The issue is not urgent since you can still use require, right? Since I am really busy right now I hope I still can find some time to look into it rather sooner than later.

ericmwalsh commented 8 years ago

Just a heads up I am using babel for transpiling ES6 and import request from 'request-promise-native'; works without a problem.

analog-nico commented 8 years ago

Thanks @ericmwalsh , good to know!

michaeljota commented 7 years ago

Maybe now that Node v0.12 has reach EOL this can be done?

sverweij commented 7 years ago

Just tried import * as request from "request-promise-native"; both with typescript (with @types/request-promise-native installed) and ES6 (via babel) and both work without a hitch.

IOW request-promise-native already supports it & @analog-nico, I think you can close this issue.

EvanCarroll commented 5 years ago

Try using node with --experimental-modules

echo "import * as request from 'request-promise-native';" > index.mjs
node --experimental-modules index.mjs

Currently getting,

(node:3105) ExperimentalWarning: The ESM module loader is experimental.
TypeError: Cannot read property 'onReady' of undefined
    at Module.load (internal/modules/cjs/loader.js:631:22)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/home/ecarroll/code/toponym-esque/node_modules/psl/index.js:14:19)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
EvanCarroll commented 5 years ago

Really though it seems like this whole approach is wrong. Moving forward at some point for posterity I would expect request.pm to work with async, await, and to adopt ES6 module syntax.

kj800x commented 5 years ago

@EvanCarroll This appears to be a regression in node 11.4.0 rather than an issue in request-promise-native. If you switch to using 11.3.0 the issue goes away.

kevin@rayquaza:~/tmp$ nvm use v11.3.0
Now using node v11.3.0 (npm v6.4.1)

kevin@rayquaza:~/tmp$ node --experimental-modules index.mjs 
(node:13156) ExperimentalWarning: The ESM module loader is experimental.

kevin@rayquaza:~/tmp$ nvm use v11.4.0
Now using node v11.4.0 (npm v6.4.1)

kevin@rayquaza:~/tmp$ node --experimental-modules index.mjs 
(node:13310) ExperimentalWarning: The ESM module loader is experimental.
TypeError: Cannot read property 'onReady' of undefined
    at Module.load (internal/modules/cjs/loader.js:631:22)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:659:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/home/kevin/tmp/node_modules/psl/index.js:14:19)
    at Module._compile (internal/modules/cjs/loader.js:723:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)

kevin@rayquaza:~/tmp$ 

I've filed an issue with core node here: https://github.com/nodejs/help/issues/1717

tbjgolden commented 5 years ago

If anyone is still struggling with this and isn't using a transpiler that handles this, here is an example of how babel manages to get both es6 imports and require to work:

https://babeljs.io/repl/#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=JYWwDg9gTgLgBAG2gUxHAZlCaBEwwDOAriDgNxA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2%2Cflow%2Cenv&prettier=false&targets=Node-9.9&version=7.4.4&externalPlugins=

import lorem from "ipsum";

becomes

var _ipsum = _interopRequireDefault(require("ipsum"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
jocubeit commented 5 years ago
// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "strictNullChecks": false,
    "target": "es2015"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}
// package.json
{
    "scripts": {
        "lint": "tslint --project tsconfig.json",
        "build": "tsc",
        ...
    },
    "dependencies": {
        "request-promise-native": "^1.0.7",
        ...
    },
    "devDependencies": {
        "@types/node": "^12.0.2",
        "@types/request-promise-native": "^1.0.16",
        "tslint": "^5.12.0",
        "typescript": "^3.5.1",
        ...
    }
}
import * as request from 'request-promise-native';

const uri = 'https://api.example.com';
const options = {
      headers: {
          'User-Agent': 'CustomAPI'
      },
      json: true
};

request(uri, options)
      .then(response => {
          console.log('response', response);
          return response;
      })
      .catch(error => {
          return error;
      });
// Local global version of typescript is same as imported dependency in package.json
$ tsc -v
Version 3.5.1

$ node -v
v12.3.1

Works for me on Firebase Cloud Functions, and I'm not using babel. I'm using the Node 8 engine on Firebase Cloud Functions for async/await support.

jrgleason commented 5 years ago

I am having the same issue when I am not using any compiler (babel, etc)