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

Error importing CommonJS module that works with --experimental-modules #9938

Open
dandv opened this issue May 4, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@dandv
Copy link

commented May 4, 2019

Bug Report

Current Behavior
I'm trying to use the default import with node-influx. This works with node --experimental-modules, but not with Babel and the transform-modules-commonjs plugin.

Input Code

import-influx.mjs

import InfluxDB from 'influx';
console.log(InfluxDB.FieldType.STRING);  // 2

Expected behavior/code

$ node --experimental-modules import-influx.mjs (node:3614) ExperimentalWarning: The ESM module loader is experimental.
2
$ ./node_modules/.bin/babel-node ./import-influx.mjs 
/home/dandv/prg/v12/a/import-influx.mjs:7
console.log(_influx.default.FieldType.STRING); // 2
                            ^

TypeError: Cannot read property 'FieldType' of undefined

Babel Configuration (babel.config.js)

module.exports = {
  plugins: [
    '@babel/plugin-transform-modules-commonjs',
  ],
};

Environment

  • Babel version(s): 7.4.4
  • Node 12/npm 6.9.0
  • OS: Ubuntu 18
  • Monorepo: no
  • How you are using Babel: cli (babel-node)
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented May 4, 2019

Hey @dandv! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented May 4, 2019

That module doesn't have a default export; only named imports: https://github.com/node-influx/node-influx/blob/master/src/index.ts.

With node you can only import it as default because node doesn't know about Babel/TypeScript/Webpack way of handling transpiled es modules.

@dandv

This comment has been minimized.

Copy link
Author

commented May 4, 2019

I found the same problem with other modules, e.g. faast.js and graphql-tools.

Is there a TypeScript transpilation setting that these modules could enable?

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented May 4, 2019

They could this code to their entry point

import * as mod from "./this-file.js";
export default mod;

By doing so, everythins is exported both as a named export and as a property of the default export.

Another option is that they also ship untranspiled ES modules (which should not
be done until they are no more --experimental), so that you can properly use named imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.