Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use with NodeJS #704

Closed
mattblickem-evoluno opened this issue Apr 5, 2024 · 7 comments
Closed

Cannot use with NodeJS #704

mattblickem-evoluno opened this issue Apr 5, 2024 · 7 comments

Comments

@mattblickem-evoluno
Copy link
Contributor

mattblickem-evoluno commented Apr 5, 2024

Cannot import strophe.js using NodeJS

See

https://github.com/mattblickem-evoluno/replicate-strophejs-nodejs-import/blob/main/strophe.js

npm i
node strophe.js
replicate-strophejs-nodejs-import/node_modules/strophe.js/src/index.js:3
import Strophe from './core.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (replicate-strophejs-nodejs-import/strophe.js:2:21)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)

If we apply the following to strophe.js/package.json:

  "exports": {
    "node": {
      "import": "./dist/strophe.esm.js",
      "require": "./dist/strophe.common.js"
    }
  },

Then the import works, but another issue hits:

TypeError: _node.querySelector is not a function
    at Request.getResponse (replicate-strophejs-nodejs-import/node_modules/strophe.js/dist/strophe.common.js:3836:88)
    at Bosh._reqToData (replicate-strophejs-nodejs-import/node_modules/strophe.js/dist/strophe.common.js:4510:18)
    at Connection._connect_cb (replicate-strophejs-nodejs-import/node_modules/strophe.js/dist/strophe.common.js:2556:49)
    at Bosh._onRequestStateChange (replicate-strophejs-nodejs-import/node_modules/strophe.js/dist/strophe.common.js:4351:7)
    at XMLHttpRequest.dispatchEvent (replicate-strophejs-nodejs-import/node_modules/xhr2/lib/xhr2.js:76:20)
    at XMLHttpRequest._setReadyState (replicate-strophejs-nodejs-import/node_modules/xhr2/lib/xhr2.js:422:14)
    at XMLHttpRequest._onHttpResponseEnd (replicate-strophejs-nodejs-import/node_modules/xhr2/lib/xhr2.js:616:14)
    at IncomingMessage.<anonymous> (replicate-strophejs-nodejs-import/node_modules/xhr2/lib/xhr2.js:568:23)
    at IncomingMessage.emit (node:events:526:35)
    at endReadableNT (node:internal/streams/readable:1589:12)

querySelector is not a function because it is not implemented by the XML parsing library.

This can be fixed by doing node.tagName === 'parsererror' instead.

Replication

See https://github.com/mattblickem-evoluno/replicate-strophejs-nodejs-import

PR

I am happy to provide a PR to fix

@mattblickem-evoluno
Copy link
Contributor Author

@jcbrand cheers for merge! is an npm release on the horizon?

@tari
Copy link

tari commented Apr 20, 2024

+1 to a patch release here; I seem to have the same issue, where I'm writing ES modules and node complains that strophe.js 2.0.0 is a CommonJS module when I import from it.

@jcbrand
Copy link
Contributor

jcbrand commented May 7, 2024

I've just made a release. Thanks for your patience.

@jcbrand jcbrand closed this as completed May 7, 2024
@jcbrand
Copy link
Contributor

jcbrand commented May 7, 2024

Unfortunately I found a bug in the package.json.

Could you please check whether the 3.0.0 release works for you, and whether this followup commit breaks anything?
120807d

Thanks 🙏

@thibautsabot
Copy link
Contributor

Hello @jcbrand!

The v3.0.0 release works fine with NodeJS, however the followup commit is breaking it again.
In my case, I need to make it work with both node and react (one import from Jest and the other one in a NextJS app with SSR).

To make it behave properly I changed the package.json with a mix of the two:

  "exports": {
    "node": {
      "import": "./dist/strophe.esm.js",
      "require": "./dist/strophe.common.js"
    },
    "default": "./src/index.js"
  }

The idea comes from https://webpack.js.org/guides/package-exports/#providing-commonjs-and-esm-version-stateless.

@jcbrand
Copy link
Contributor

jcbrand commented Jul 2, 2024

Hi @thibautsabot, can you please make a pull request with your change? Thanks 🙏

@thibautsabot
Copy link
Contributor

Sure! #712

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants