Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cajoy opened this issue Jul 21, 2016 · 15 comments
Open

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

cajoy opened this issue Jul 21, 2016 · 15 comments

Comments

@cajoy
Copy link

cajoy commented Jul 21, 2016

No description provided.

@analog-nico
Copy link
Member

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
Copy link
Author

cajoy commented Jul 21, 2016

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
Copy link
Member

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
Copy link
Author

cajoy commented Jul 21, 2016

  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
Copy link
Member

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
Copy link

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

@analog-nico
Copy link
Member

Thanks @ericmwalsh , good to know!

@michaeljota
Copy link

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

@sverweij
Copy link

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
Copy link

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
Copy link

EvanCarroll commented Jan 2, 2019

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
Copy link

kj800x commented Jan 13, 2019

@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: nodejs/help#1717

@tbjgolden
Copy link
Contributor

tbjgolden commented May 12, 2019

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
Copy link

jocubeit commented Jun 2, 2019

// 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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

11 participants