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

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)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source