Comments (5)
Please use commonjs
from node-minecraft-protocol.
You are importing commonjs with ESM, node.js only supports importing commonjs exports as a default export when imported in an ESModule.
from node-minecraft-protocol.
Alright, I see. I created this issue, because I could import everything else the ESM way with no issues, so ping
was the outlier to me.
from node-minecraft-protocol.
The import syntax does not do destructuring, despite the similar syntax. It instead looks up objects against the module's ESM exports list. The technical reason for this is that ESM export/import lookups are done statically in the parse step before the program is run. This is so browsers can asynchronously load all the dependencies for a page without needing to synchronously run the code and fetch the deps one by one. With CommonJS only an object is exported (or a function/variable under the module.export variable), that's translated to the default
export in ESM. Now setting aside CommonJS translation, the default
export in ESM is intended for when you want to export an anonymous function or variable from a file, which would be named by the caller. Like the error message says you would need to use a different import syntax for destructuring:
import pkg from 'minecraft-protocol'; // pkg gets the default export variable and assigns it to pkg
const { ping } = pkg; // this actually does destructuring to get the ping function from pkg object
When you do a "namespace import" like:
import * as pkg from 'pkg'
You are binding all the ESM exports into pkg, including the default
one.
So why can you do import {x, y, z} from 'pkg'
or import * as pkg from 'pkg'; pkg.x
sometimes as opposed to using the default object in Node.js? That's because Node.js added a compatibility feature to try and export all the CommonJS exports as ESM exports. But again since all the ESM import/export are done statically, that compatibility feature works without running the code. So it can only use static analysis to try and make the CommonJS module look like an ESM one before the code is ran. You can see this for yourself by adding a console.log to the top of node_modules/minecraft-protocol/index.js and checking that error on importing ping -- no log will be made. If the static translator gets stuck, it will give up trying to promote things inside module.export to ESM exports, so you must use the default export object and destructure at runtime.
from node-minecraft-protocol.
I see. Thank you for the comprehensive explanation and apologies for my confusion!
from node-minecraft-protocol.
Related Issues (20)
- Cannot read properties of undefined (reading 'overworld') HOT 1
- "Chunk size is 42 but only 2 was read" error HOT 7
- Inconsistent `playerChat` event data
- undefined nbt value HOT 2
- Some event handlers end up being called multiple times after going thru configuration state. HOT 1
- 1.20.4 Login event never triggered on Node 18 HOT 19
- versionChecking.js Bug HOT 4
- TypeError: RSA_PKCS1_PADDING is no longer supported for private decryption HOT 7
- Error: Failed to obtain profile data for X, does the account own minecraft?
- Ladders arent working HOT 3
- mc.createClient doesnt check for SRV dns records HOT 1
- Is there any way to modify the protocol on server side? (maybe using fabric or spigot) HOT 1
- pickRealm silently fails to connect HOT 6
- Entity Effect proxy
- Error: unsupported protocol version: 1.20.6
- Minecraft 1.21+ support HOT 2
- Read Packets from other sources HOT 4
- Problems with Deserializer HOT 3
- Player count modify HOT 1
- Vanilla client cannot connect to example server
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-minecraft-protocol.