Coder Social home page Coder Social logo

Comments (5)

rom1504 avatar rom1504 commented on August 24, 2024

Please use commonjs

from node-minecraft-protocol.

PondWader avatar PondWader commented on August 24, 2024

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.

Opisek avatar Opisek commented on August 24, 2024

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.

extremeheat avatar extremeheat commented on August 24, 2024

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.

Opisek avatar Opisek commented on August 24, 2024

I see. Thank you for the comprehensive explanation and apologies for my confusion!

from node-minecraft-protocol.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.