LeaVerou/parsel

Failed to use this module in vanilla node.js es modules.

hyrious opened this issue · 1 comments

Repro:

mkdir test && cd test
npm init -y && npm i parsel-js
echo 'import parsel from "parsel-js"' > index.mjs
node index.mjs

(node:32143) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
...
SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)

Node.js chooses esm loader over cjs loader in these 2 conditions:

  1. the filename ends with ".mjs".
  2. package.json has "type": "module", which means all .js files in this folder are treated as es modules.

So if you want this library be used in vanilla node.js es modules, the best way I can tell is to change the esm output file extension to .mjs.

As a workaround, use this:

import * as parsel from "./node_modules/parsel-js/dist/parsel.min.js"

This seems to be resolved using Dual CommonJS/ES packages in this commit

So I guess this issue could be closed