Open cajoy opened 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 :(
Unfortunately, I don't have a test set up for that so please give me more input:
import request from 'request-promise-native'
work?import * as request from 'request-promise-native'
produce?import * as request from 'request-promise-native'
transpiled into?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)
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.
Just a heads up I am using babel for transpiling ES6 and
import request from 'request-promise-native';
works without a problem.
Thanks @ericmwalsh , good to know!
Maybe now that Node v0.12 has reach EOL this can be done?
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.
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)
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.
@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
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:
import lorem from "ipsum";
becomes
var _ipsum = _interopRequireDefault(require("ipsum"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// 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.
I am having the same issue when I am not using any compiler (babel, etc)
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: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.