repetere/jsonx

Cannot use library in nodejs environment

dimensi opened this issue · 1 comments

If I try import library in commonjs style (i set in packages.json type: "commonjs")

const jsonx = require('jsonx')

I got that error

internal/modules/cjs/loader.js:1092
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/dimensi/projects/parse-archives/node_modules/jsonx/dist/index.cjs.js
require() of ES modules is not supported.
require() of /Users/dimensi/projects/parse-archives/node_modules/jsonx/dist/index.cjs.js from /Users/dimensi/projects/parse-archives/render-jsx.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.cjs.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/dimensi/projects/parse-archives/node_modules/jsonx/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:13)
    at Module.load (internal/modules/cjs/loader.js:940:32)
    at Function.Module._load (internal/modules/cjs/loader.js:781:14)
    at Module.require (internal/modules/cjs/loader.js:964:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/dimensi/projects/parse-archives/render-jsx.js:2:15)
    at Module._compile (internal/modules/cjs/loader.js:1075:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:10)
    at Module.load (internal/modules/cjs/loader.js:940:32)
    at Function.Module._load (internal/modules/cjs/loader.js:781:14) {

If I try import library in esm style (I set type: 'module' in package.json). I got next

import jsonx from 'jsonx'
import jsonx from 'jsonx'
       ^^^^^
SyntaxError: The requested module 'jsonx' does not provide an export named 'default'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
    at async Loader.import (internal/modules/esm/loader.js:165:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

If I try import with as

import * as jsonx from 'jsonx
Object.defineProperty(exports, '__esModule', { value: true });
                      ^

ReferenceError: exports is not defined
    at file:///Users/dimensi/projects/parse-archives/node_modules/jsonx/dist/index.cjs.js:3:23
    at ModuleJob.run (internal/modules/esm/module_job.js:140:23)
    at async Loader.import (internal/modules/esm/loader.js:165:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

I don't know how to import package in nodejs...

Also I try use parcel for import package and I got here another problem:

console.log(outputJSX({
  component:'p',
  props:{ style:{ color:'blue' } },
  children:'hello world'
})) -> <p style={[Object object]}>hello world</p>

you should be able to explicitly load the CJS build https://github.com/repetere/jsonx/blob/main/dist/index.cjs.js

const jsonx = require('jsonx/dist/index.cjs.js')