marked icon indicating copy to clipboard operation
marked copied to clipboard

module import/export issue

Open paddotk opened this issue 5 years ago • 59 comments

Marked version: 4.0.0

Describe the bug I updated marked in our project from ^3.0.2 to 4.0.0 (before it worked fine), and after the update I get an error marked__WEBPACK_IMPORTED_MODULE_5___default(...) is not a function when using it. It seems that the module is no longer exported properly, I tried to change the import import { marked } from 'marked' as well as using marked.parse() as shown in the readme, but that doesn't seem to help.

To Reproduce Steps to reproduce the behavior: Use a webpack/npm project, import by means of import marked from 'marked'. Use the marked() function.

paddotk avatar Nov 03 '21 16:11 paddotk

It looks like somewhere in your code you are trying to import the default export like import marked from 'marked'.

Make sure you replace all occurrences with import { marked } from 'marked'

UziTech avatar Nov 03 '21 17:11 UziTech

I'm using TypeScript and can also confirm that I am also experiencing the same issue on 4.0.0. I am importing it using import { marked } from "marked"; but getting 'marked' can only be imported using 'import marked = require("marked")' or a default import.. It is possible that I just need to wait until @types/marked is bumped up a major version though.

AlanMorel avatar Nov 04 '21 05:11 AlanMorel

@UziTech I tried that but didn't work... @AlanMorel From the error it looks like a Webpack issue. It also shows in my unit tests which are .js files.

paddotk avatar Nov 04 '21 08:11 paddotk

It seems unlikely to be a webpack-specific issue because the project I'm using Marked on is not using webpack at all.

AlanMorel avatar Nov 04 '21 14:11 AlanMorel

It is possible that I just need to wait until @types/marked is bumped up a major version though.

I have the same problem and @types/marked is still in 3.X

maurelio1234 avatar Nov 04 '21 15:11 maurelio1234

@types/marked v4 should be released soon

UziTech avatar Nov 04 '21 17:11 UziTech

Great, I believe that update to @types/marked will resolve my issue, so I will wait for that, thanks!

Side question though: any reason marked itself is not providing the typings for the library? It would both eliminate needing a separate @types/marked dependency while also ensuring that you can bump both the library and typings at the same time. Not a huge deal though, just curious.

AlanMorel avatar Nov 04 '21 18:11 AlanMorel

Also waiting for @types/marked ... Do you guys have a workaround or maybe I should go back to previous stable version?

rtjtc avatar Nov 04 '21 18:11 rtjtc

Pretty sure your only options are wait for the updated types (which should be coming soon) or use a previous version.

AlanMorel avatar Nov 04 '21 19:11 AlanMorel

Ok, thanks. If it is the typings that cause the issue though, is there any way to omit the blocking error for now? Such as placing a // @ts-ignore comment somewhere?

paddotk avatar Nov 05 '21 10:11 paddotk

@paddotk If you are in a hurry you can always use gitpkg.now.sh and get an NPM package from the latest commit of UziTech's PR while waiting for it to be merged.

"@types/marked": "https://gitpkg.now.sh/UziTech/DefinitelyTyped/types/marked?415785793c958a98fa8902c482fe8be7959f86f6"

jfcere avatar Nov 07 '21 02:11 jfcere

The types have been merged and it's looking good on my end now

AlanMorel avatar Nov 09 '21 01:11 AlanMorel

It seems like v4.0.0 still doesn't work with Webpack. It looks like Webpack is trying to import lib/marked.cjs as an ES6 module. If I manually remove the "browser" settings in package.json or set it to be the same as "main", webpack works. I guess this stems from the pre-ES6 module PR #1661, but I think it's safe to at least revert this PR for the short term until a better solution is available, if it causes webpack to not work at all...

boazy avatar Nov 09 '21 12:11 boazy

looks like webpack suggests this fix.

@boazy if you want to test that out and create a PR for that we could get it merged in.

UziTech avatar Nov 09 '21 19:11 UziTech

I'm also having issues. I'm not using webpack, typescript, babel or anything, just using ESM module to run my code in Express.

v3 works correctly, if I upgrade to v4 and switch my import from import marked from 'marked'; to import { marked } from 'marked'; I get the following error trying to run it:

/<path>/app/shared/text/format.js:1
Error [ERR_REQUIRE_ESM]: require() of ES Module /<path>/node_modules/marked/lib/marked.esm.js not supported.
Instead change the require of marked.esm.js in null to a dynamic import() which is available in all CommonJS modules.

scottlet avatar Nov 10 '21 06:11 scottlet

I also have the same issue when using marked in CodeSandbox.

When using [email protected], and trying to import { marked } from 'marked (using the latest @types/[email protected]), marked is undefined. For now I've resorted to using [email protected] with import marked from 'marked. But it would e nice to have this fixed.

rfgamaral avatar Nov 10 '21 09:11 rfgamaral

marked 4.0.1 has been released that may help with ESM instead of commonJS issues. The main entry in package.json now points to the commonJS code.

Real node ESM modules using babel will need to configure for it (unrelated to first comment).

webstech avatar Nov 11 '21 06:11 webstech

It seems to fix my issue, thanks!

scottlet avatar Nov 11 '21 07:11 scottlet

@webstech This still doesn't work for me:

const INITIAL_MARKED_OPTIONS = {
    gfm: false,
    headerIds: false,
}

// Reset Marked to the defaults and set custom options
marked.setOptions({
    ...marked.getDefaults(),
    ...INITIAL_MARKED_OPTIONS,
})

image

rfgamaral avatar Nov 11 '21 09:11 rfgamaral

@webstech This still doesn't work for me:

Did you restart the editor? The types change requires that for some setups (vscode/eslint in my case). Are you getting compile errors? Pasted your code into mine and there were no complaints. _marked.marked does not show up in a search. Do you know where this is coming from?

webstech avatar Nov 11 '21 17:11 webstech