Node.jsでimport { foo } はCommonJSモジュールでは使用不可

Node.jsのimport { foo }でエラー

Node.jsを使用する際にrequireではなくimportが使用されることが多いが、import { foo }のようにimportするとCommonJSモジュールだと以下のようにSyntaxError: Named exportのエラーになる。(以下はiconv-liteの場合)

import { encode } from 'iconv-lite'

console.log(encode)
SyntaxError: Named export 'encode' not found. The requested module 'iconv-lite' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'iconv-lite';
const { encode } = pkg;

シンタックスエラーの文章にも書かれているがCommonJSモジュールだと直接 { encode }
で取得できない
ので一度 {} を使わずにimportしてconst { encode }の形で取得しなけれがならない。

import iconv from 'iconv-lite'
const { encode } = iconv

console.log(encode)

※ pkgの名前は任意なので前述の例ではiconvにしてあります。