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

Closed
dandv opened this issue May 4, 2019 · 4 comments
Closed
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@dandv
Copy link

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

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

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

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

nicolo-ribaudo 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.

@JLHwung JLHwung closed this as completed Nov 25, 2019
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Feb 25, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants