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

Issues with import into es6 project #153

Open
RobMaskell opened this issue May 3, 2019 · 24 comments
Open

Issues with import into es6 project #153

RobMaskell opened this issue May 3, 2019 · 24 comments

Comments

@RobMaskell
Copy link

I've pulled this into my project from NPM and I'm trying to import it into a webcomponent but

import { humanizeDuration } from 'humanize-duration/humanize-duration.js'; gives an error that there is not export named humanizeDuration

So I tried

import 'humanize-duration/humanize-duration.js'; but this gives an error Uncaught (in promise) TypeError: Cannot set property 'humanizeDuration' of undefined from the following line 1118 code

  humanizeDuration.humanizer = humanizer;

  if (typeof define === 'function' && define.amd) {
    define(function () {
      return humanizeDuration;
    });
  } else if (typeof module !== 'undefined' && module.exports) {
    module.exports = humanizeDuration;
  } else {
    this.humanizeDuration = humanizeDuration;
  }
})(); // eslint-disable-line semi

basically the last line of real code in the above snippet.

Any ideas how to get around it?

@EvanHahn
Copy link
Owner

EvanHahn commented May 3, 2019

This seems like an issue with how CommonJS imports are being rewritten with imports.

Does import humanizeDuration from 'humanize-duration work?

@RobMaskell
Copy link
Author

Fair shout, I gave it a try and this was the result Uncaught (in promise) SyntaxError: The requested module '../../node_modules/humanize-duration/humanize-duration.js' does not provide an export named 'default'

@EvanHahn
Copy link
Owner

EvanHahn commented May 3, 2019 via email

@RobMaskell
Copy link
Author

RobMaskell commented May 3, 2019

It's a polymer app, so currently just running with npm start without any transpiling, babel used for prod though

@EvanHahn
Copy link
Owner

EvanHahn commented May 4, 2019

Okay. Are you able to import any other CommonJS modules this way?

@RobMaskell
Copy link
Author

I seem to remember having similar issues with time-elements that I also use, but I think they now have two versions and modern one and a legacy.
I did try adding the script tag to index.html and while that does work in dev it breaks when the proper build script runs which is what reminded me of time-elements.
I think the polymer dev server does something for imports in terms of rewriting them but I'm not sure exactly what.

@EvanHahn
Copy link
Owner

EvanHahn commented May 4, 2019

Could you try writing a simple CommonJS module and using it? Here's a simple one:

module.exports = function () {
  return 'Hello world!'
}

My guess is that this isn't a problem with Humanize Duration but instead a problem with some module setup.

@RobMaskell
Copy link
Author

Hmmm I just found this PWA Stater Kit FAQ maybe it just needs a ESM. I'll have a play with script tags and see if I can make it work.

andrew-c-tran pushed a commit to andrew-c-tran/HumanizeDuration.js that referenced this issue Jul 25, 2019
@andrew-c-tran
Copy link

@RobMaskell see #159

@RobMaskell
Copy link
Author

Thanks @andrew-c-tran I'll check this out at the weekend when I have a little more time

@RobMaskell
Copy link
Author

Ok I tried and changed my imports as follows

import { LitElement, html } from 'lit-element';

import * as humanizeDuration from 'humanize-duration';

import '@polymer/marked-element/marked-element.js';
import '@polymer/paper-spinner/paper-spinner-lite.js'
import '@polymer/paper-fab/paper-fab.js';
import '@polymer/iron-icons/iron-icons.js';
import '@polymer/iron-icons/device-icons.js';

I get the following error

humanize-duration.js:1185 Uncaught (in promise) TypeError: Cannot set property 'humanizeDuration' of undefined
    at humanize-duration.js:1185
    at humanize-duration.js:1187

it doesn't like the line

this.humanizeDuration = humanizeDuration;

@EvanHahn
Copy link
Owner

@RobMaskell I suspect this is an issue with your build setup. How are you bundling dependencies?

@RobMaskell
Copy link
Author

RobMaskell commented Jul 28, 2019

I call npm start which runs prpl-server --root server/build which I guess is local dev web server node I suspect for serving sites without much packaging for dev purposes. It all comes from https://github.com/Polymer/pwa-starter-kit but I've had no issues with any other imports although I take your point that it's probably some interaction between this server and your code.

@EvanHahn
Copy link
Owner

EvanHahn commented Jul 28, 2019 via email

@RobMaskell
Copy link
Author

RobMaskell commented Jul 28, 2019

I had the same problem back in the day with https://github.com/github/time-elements and then I think they sorted it in a new version, might be worth a look?

@EvanHahn
Copy link
Owner

Looks like they publish two exports and use the not-standard-but-used module key in package.json. Not exactly sure what this library should do yet, but wanted to comment about my findings.

@EvanHahn
Copy link
Owner

Node has an official recommendation not to publish ES modules right now. That may change in the future, but for now, I think I'm going to do nothing.

@rokerkony
Copy link

Please do not publish any ES module packages intended for use by Node.js until this is resolved.

But it can be used on frontend without node as well...

@EvanHahn
Copy link
Owner

EvanHahn commented Aug 26, 2019 via email

@EvanHahn
Copy link
Owner

Revisiting this, it seems like Node is still working on ES module support. Is anyone aware of the best practices here?

@dasa
Copy link

dasa commented Jul 20, 2021

Sindre Sorhus has been moving his packages to pure ESM.

@MMK21Hub
Copy link

MMK21Hub commented Aug 2, 2022

This kind of import works correctly when using Vite + TypeScript:

import humanizeDuration, { HumanizerOptions } from "humanize-duration"

The @types/humanize-duration package provides typings.

@MagnusOxlund
Copy link

Sindre Sorhus has been moving his packages to pure ESM.

Looks like a dead link from here.

@millette
Copy link

@MagnusOxlund @dasa Sindre's blog post was removed (why?), here's an archive: https://archive.is/5zMdR

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

No branches or pull requests

8 participants