Skip to content

[v5] Need exports on Package.json for ESM support #61

Open
@axmad386

Description

@axmad386

This is issue for version 5 (dev).
I currently use node-input-validator version next (v5). I love this next version, cannot wait for the stable release 😃 .
And the problem begin when I use it on esmodule.
This is the error throw when I import it from esmodule

import { extend, Messages, Validator } from 'node-input-validator';
         ^^^^^^
SyntaxError: The requested module 'node-input-validator' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'node-input-validator';
const { extend, Messages, Validator } = pkg;

Currently when I import node-input-validator, it still import the cjs version.

The fix is just add additional config "exports" in package.json
example

"main": "index",  
"module": "index.mjs",
"exports": {
    ".": {
        "import": "./esm/index.js",
        "require": "./cjs/index.js"
    }
},

Also you must add additional package.json on each folder with this bash script

#!/bin/sh
cat >cjs/package.json <<!EOF
{
    "type": "commonjs"
}
!EOF

cat >esm/package.json <<!EOF
{
    "type": "module"
}
!EOF

You can read more here how to make hybrid nodejs package

For now, I am still use the cjs version and import it with @rollup/plugin-commonjs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions