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

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

Closed
dennisgeerts opened this issue May 4, 2022 · 2 comments

Comments

@dennisgeerts
Copy link

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)

@coen22
Copy link

coen22 commented Aug 12, 2022

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

@mdevils
Copy link
Owner

mdevils commented Aug 15, 2022

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 mdevils closed this as completed Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants