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

Enable es6 import in Node.js #17

Closed
clibu opened this issue Mar 4, 2021 · 7 comments
Closed

Enable es6 import in Node.js #17

clibu opened this issue Mar 4, 2021 · 7 comments

Comments

@clibu
Copy link

clibu commented Mar 4, 2021

I'm writing all my Node.js code using es6 import's however AceBase currently needs the following hack for import to work.

import pkg from 'acebase';
const { AceBase } = pkg;
@appy-one
Copy link
Owner

appy-one commented Mar 4, 2021

Why not just use

import { AceBase } from 'acebase';

@clibu
Copy link
Author

clibu commented Mar 4, 2021

import { AceBase } from 'acebase';
         ^^^^^^^
SyntaxError: The requested module 'acebase' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can
be imported by importing the default export.
For example:
import pkg from 'acebase';
const { AceBase } = pkg;

@clibu
Copy link
Author

clibu commented Mar 4, 2021

This is using "type": "module", in package.json

@appy-one
Copy link
Owner

appy-one commented Mar 4, 2021

I'm not able to reproduce?
If I set "type": "module" in my test package.json, I am able to use import { AceBase } from 'acebase'; without getting an error.
What node version are your using? I just tested with v14.15.4

@appy-one
Copy link
Owner

appy-one commented Mar 4, 2021

Apparently this is possible since Node v14.13.0, see this tweet, or this blog post about it: Node.js now supports named imports from CommonJS modules, but what does that mean?

@clibu
Copy link
Author

clibu commented Mar 4, 2021

I am using 14.7.0 which works fine with es6 modules. I've just updated to 15.11.0 and import works now.

Whether you want to try and support older versions of Node is a question for you.😀

Did you notice my discussion post: #13

@appy-one
Copy link
Owner

appy-one commented Mar 4, 2021

Sorry, didn't see the discussion earlier. Somehow the GitHub notification emails are not always getting through...

AceBase supports older Node versions, I haven't checked it but should at least be compatible back to version 7. If I switch to using ES modules, that would mean dropping support for older Node versions. And with the Node team already having fixed mixing CommonJS / ES modules, I'd say "don't fix it if it ain't broken"! 😄

@appy-one appy-one closed this as completed Mar 5, 2021
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

2 participants