node-input-validator icon indicating copy to clipboard operation
node-input-validator copied to clipboard

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

Open axmad386 opened this issue 4 years ago • 2 comments

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

axmad386 avatar Jul 02 '21 09:07 axmad386

Thanks for the information, will add soon. FYI: Currently, I 'm working on nested inputs logic, will rollout new release soon. Stable version soon after this.

bitnbytesio avatar Jul 15 '21 09:07 bitnbytesio

Hi, glad to hear that. Thanks for your hardworking. Cannot wait for the stable version 👍 btw, thanks for merging my PR

axmad386 avatar Jul 16 '21 11:07 axmad386