Skip to content

Doesn't work with babel #501

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

Closed
panuhorsmalahti opened this issue Sep 6, 2016 · 10 comments
Closed

Doesn't work with babel #501

panuhorsmalahti opened this issue Sep 6, 2016 · 10 comments
Labels

Comments

@panuhorsmalahti
Copy link

panuhorsmalahti commented Sep 6, 2016

node-oracledb doesn't work when compiling ES6 modules with Babel. I'm using TypeScript + Babel, but I don't believe the use of TypeScript is relevant in this bug.

The following ES6 code doesn't work:

import * as OracleDB from "oracledb";

export const getConnection = () => {
    const promise = OracleDB.getConnection({
        user: "user",
        password: "pw",
        connectString
    }).then(conn => {

    });
};

If I don't use _interopRequireWildcard the function works.

Doesn't work (compiled ES5 JavaScript):

var _oracledb = require("oracledb");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

var OracleDB = _interopRequireWildcard(_oracledb);

The following works:

var OracleDB = require("oracledb");

Answer the following questions:

  1. What error(s) you are seeing?
TypeError: Cannot read property 'Promise' of undefined
    at Object.getConnection (/Users/pahorsma/dev/app/node_modules/oracledb/lib/util.js:96:24)

The error originates from this line:
https://github.com/oracle/node-oracledb/blob/master/lib/util.js#L96

  1. What exact command caused the problem (e.g. what command did you try to install with)?
 OracleDB.getConnection()
  1. What environment variables did you set? How exactly did you set them?
    None
  2. What is your version of Node.js?
    6.4.0
  3. What is your version of the Oracle client (e.g. Instant Client)? How was it installed? Where it is installed?
    instantclient 12.1.0.2.0
  4. What is your OS and version?
    Mac OS X 10.11.6
  5. What compiler version did you use?
    Latest XCode
@cjbj cjbj added the question label Sep 6, 2016
@cjbj
Copy link
Member

cjbj commented Sep 6, 2016

For the non-Babel readers (like me), can you give some more context, some setup hints, and working & non working testcases?

@panuhorsmalahti
Copy link
Author

panuhorsmalahti commented Sep 7, 2016

Here's a minimal setup to reproduce the error:

First install babel-cli:

npm install -g babel-cli

Save the following as test.js:

import * as OracleDB from "oracledb";
OracleDB.getConnection();

Run the file with babel-node test.js.

The output:

pahorsma app $ babel-node test.js 
/Users/pahorsma/dev/app/node_modules/oracledb/lib/util.js:96
    if (!self._oracledb.Promise || typeof arguments[arguments.length - 1] === 'function') {
                       ^

TypeError: Cannot read property 'Promise' of undefined
    at Object.getConnection (/Users/pahorsma/dev/app/node_modules/oracledb/lib/util.js:96:24)
    at Object.<anonymous> (/Users/pahorsma/dev/app/test.js:2:10)
    at Module._compile (module.js:556:32)
    at loader (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:146:5)
    at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:156:7)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Function.Module.runMain (module.js:590:10)
    at /usr/local/lib/node_modules/babel-cli/lib/_babel-node.js:151:24

This ES6 code can be compiled to ES5 with babel.

A working test case simply uses ES5 modules:

var OracleDB = require("oracledb");
OracleDB.getConnection();

@dmcghan
Copy link

dmcghan commented Sep 7, 2016

@panuhorsmalahti After looking into this a little, I think it has to do with the way that import in Babel differs from require when it comes to circular references (which is essentially what self._oracledb is in the error).

We should be able to put together a solution. In the meantime, please use require.

@cjbj
Copy link
Member

cjbj commented Oct 19, 2016

@dmcghan are you tracking this?

@dmcghan
Copy link

dmcghan commented Oct 19, 2016

@cjbj Yes, I have a note to look into the with the next round of enhancements.

@cjbj
Copy link
Member

cjbj commented Dec 8, 2016

@dmcghan ping

@dmcghan
Copy link

dmcghan commented Dec 15, 2016

@panuhorsmalahti Could you please try changing this line:

import * as OracleDB from "oracledb";

to this:

import OracleDB from "oracledb";

That's shorthand for this:

import {default as OracleDB} from "oracledb";

Either one should work. It's a way of telling Babel to import the default export. When using the asterisk as you were doing before, Babel used _interopRequireWildcard instead of _interopRequireDefault. It seems that _interopRequireWildcard enumerates the properties of the export to create a new object. Not all properties are enumerable which lead to the error you saw.

@cjbj
Copy link
Member

cjbj commented Jan 10, 2017

@panuhorsmalahti ping?

@panuhorsmalahti
Copy link
Author

panuhorsmalahti commented Jan 10, 2017

The import OracleDB from "oracledb"; form works fine (at least with TypeScript + Babel)! Thank you.

@cjbj
Copy link
Member

cjbj commented Jan 11, 2017

@panuhorsmalahti thanks for the feedback.

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

No branches or pull requests

3 participants