The Wayback Machine - https://web.archive.org/web/20201110153540/https://github.com/markedjs/marked/issues/1692
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

documentation to show ESM import and usage #1692

Open
henriquez opened this issue May 31, 2020 · 7 comments
Open

documentation to show ESM import and usage #1692

henriquez opened this issue May 31, 2020 · 7 comments

Comments

@henriquez
Copy link

@henriquez henriquez commented May 31, 2020

Describe the bug
I'm using rollup to bundle dependencies (for a Svelte framework web app) but having trouble importing and using markedjs. It appears the library was recently ported to ESM modules, but I don't see any documentation on how to use as such. The standard syntax to import ES6 modules give me errors:

import { marked } from 'marked.esm';
returns
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
marked.esm (imported by src/parser/parser.js)
(!) Missing global variable name
Use output.globals to specify browser global variable names corresponding to external modules
marked.esm (guessing 'marked')

and

import { marked } from 'marked';
returns
bundles src/main.js → public/build/bundle.js...
(!) this has been rewritten to undefined
https://rollupjs.org/guide/en/#error-this-is-undefined
node_modules/marked/lib/marked.js
14: typeof define === 'function' && define.amd ? define(factory) :
15: (global = global || self, global.marked = factory());
16: }(this, (function () { 'use strict';
^
17:
18: function _defineProperties(target, props) {
[!] Error: 'marked' is not exported by node_modules/marked/lib/marked.js, imported by src/parser/parser.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
src/parser/parser.js (11:9)

To Reproduce
npm install marked

in a Svelte js file that's bundled by rollup:
import { marked } from 'marked'; // or similar variations

Expected behavior
no import errors

@KilianKilmister
Copy link

@KilianKilmister KilianKilmister commented May 31, 2020

marked.esm is using a default export instead of a named export. so the proper syntax would be:

import marked from 'marked' // note that this is a definition, so you can give it any name you want

But i think you are raising an important point: Why was a default export chosen over a named export? It removes many of the advantages of using esm. As the result is still just some convoluted mess of properties.

It would be much more userfriendly with well organized named exports as they work excellently with TS-language-features.

I'll file a seperate issue about that in a bit

@henriquez
Copy link
Author

@henriquez henriquez commented May 31, 2020

I tried that syntax as well, but errors with "'default' is not exported by node_modules/marked/lib/marked.js" detail below:

bundles src/main.js → public/build/bundle.js... (!) thishas been rewritten toundefined https://rollupjs.org/guide/en/#error-this-is-undefined node_modules/marked/lib/marked.js 14: typeof define === 'function' && define.amd ? define(factory) : 15: (global = global || self, global.marked = factory()); 16: }(this, (function () { 'use strict'; ^ 17: 18: function _defineProperties(target, props) { [!] Error: 'default' is not exported by node_modules/marked/lib/marked.js, imported by src/parser/parser.js https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module src/parser/parser.js (10:7) 8: 9: /*************** Imports *****************/ 10: import marked from 'marked';'

Looking at lib/marked.esm.js#L2356 it exports default, but if I try
import marked from 'marked_esm';
that errors with
(!) Unresolved dependencies https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency marked_esm (imported by src/parser/parser.js) (!) Missing global variable name Use output.globals to specify browser global variable names corresponding to external modules marked_esm (guessing 'marked')
and in my js I get

Uncaught ReferenceError: marked is not defined

with
marked.lexer('# heading');

@UziTech
Copy link
Member

@UziTech UziTech commented Jun 1, 2020

have you tried import marked from "marked/lib/marked.esm.js";?

@henriquez
Copy link
Author

@henriquez henriquez commented Jun 7, 2020

@UziTech
Copy link
Member

@UziTech UziTech commented Jun 7, 2020

I don't usually use esm since it is still experimental in node. If anyone wants to create a PR I would be happy to review it.

@evanplaice
Copy link

@evanplaice evanplaice commented Jul 4, 2020

Here's the usage

import Marked from '../node_modules/marked/lib/marked.esm.js'

Marked(rawMarkdown)

I'm using it in the <wc-markdown> web component

@KilianKilmister
Copy link

@KilianKilmister KilianKilmister commented Jul 9, 2020

cjs-require/esm-import can be automatically handled by specifying an "exports" field in the package.json.
This way node will load the appropriate files without the user having to worry about in-module paths.

Short explanation

{
  "exports": {
    [exportPath]: {
      "import": "./path/to/file.mjs",
      "require": "./path/to/file.cjs"
    }
  }
}

So for marked it would be:

{
  "exports": {
    ".": {
      "import": "./lib/marked.esm.mjs", // `.mjs`-extention is needed
      "require": "./src/marked.js"
    }
  }
}

I'll file a PR for that shortly

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

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.