html-entities icon indicating copy to clipboard operation
html-entities copied to clipboard

Can't import the named export 'decode' from non EcmaScript module (only default export is available)

Open dennisgeerts opened this issue 3 years ago • 2 comments

I am using the html-entities package from npm and trying to import the decode method in my nodejs application. I have updated to the latest package v2.3.3

I have tried plenty of differnet methods on importing, but none seem to work, all of them give a different error:

import { decode } from 'html-entities'
let result = decode('test')

This gives the error:

Can't import the named export 'decode' from non EcmaScript module (only default export is available)

When trying with the code:

const { decode } = require('html-entities') It gives me the error:

This file is being treated as an ES module because it has a '.js' file extension and 'D:\giveaday\Give-a-Day\backend\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

And ultimately, I tried using the old way (v1):

import htmlEntities from 'html-entities'
let result = htmlEntities.decode('test')

and then it returns the error:

ERROR html_entities__WEBPACK_IMPORTED_MODULE_4__.decode is not a function

I've also tried the solution where they say to configure webpack by adding the following code in the nuxt.config.mjs:

config.module.rules.push({
    test: /\.js$/,
    include: /node_modules/,
    type: 'javascript/auto',
  })

However, this did not work either. (the nuxt.config.mjs is located in my /frontend/ folder, while the library is being imported in my /shared/ folder)

dennisgeerts avatar May 04 '22 14:05 dennisgeerts

It can be fixed easily, just change: function decode(text, _a) { to export function decode(text, _a) { and do the same for each other function in index.js

coen22 avatar Aug 12 '22 10:08 coen22

Hello @dennisgeerts.

I'm not sure I've understood your setup. You're saying that you are trying to use this package in your nodejs application, at the same time you post Webpack-related errors. Are you compiling your nodejs app with webpack?

mdevils avatar Aug 15 '22 09:08 mdevils