NateTheGreatt / bitECS

Flexible, minimal, data-oriented ECS library for Typescript
Mozilla Public License 2.0
944 stars 84 forks source link

Error: Must use import to load ES Module #30

Closed AdelNorberg closed 3 years ago

AdelNorberg commented 3 years ago

Hi! Sorry if I made a stupid mistake. My knowledge in node.js is weak :( But I don’t understand why doesn’t see module build in your package. I am getting an error in the console: image

Node v16.5.0 bitecs v0.3.15-1

package.json: image

tsconfig: image

AdelNorberg commented 3 years ago

image

If I remove this line everything works

NateTheGreatt commented 3 years ago

the best solution would be to switch from CommonJS modules (require) over to the more modern ES modules (import), as it is officially supported in the version of node you are using (16) 🙂

CommonJS:

const { createWorld } = require('bitecs')

ES:

import { createWorld } from 'bitecs'
SupremeTechnopriest commented 3 years ago

Also, you will have to either set "type": "module" in your package.json or name your files with the .mjs extension.

AdelNorberg commented 3 years ago

Thank you very much for your reply. On the screenshot there is a property { type: "module" } and and I am using import instead of require. But the error still remains. At the same time, I have no problems with other packages (uWebSockets.js, three.js, and more)

the best solution would be to switch from CommonJS modules (require) over to the more modern ES modules (import), as it is officially supported in the version of node you are using (16) 🙂

CommonJS:

const { createWorld } = require('bitecs')

ES:

import { createWorld } from 'bitecs'
SupremeTechnopriest commented 3 years ago

See my above comment. You need to give your js files the .mjs extension or add "type": "module" to your own package json.

AdelNorberg commented 3 years ago

See my above comment. You need to give your js files the .mjs extension or add "type": "module" to your own package json.

But I have addedtype: "module". You can see it in the screenshot above

SupremeTechnopriest commented 3 years ago

Did you make sure to add it back to the bitecs package.json? Are you using a bundle like webpack or rollup?

NateTheGreatt commented 3 years ago

On the screenshot there is a property { type: "module" } and and I am using import instead of require. But the error still remains. At the same time, I have no problems with other packages (uWebSockets.js, three.js, and more)

I suspect you are using a bundler like webpack which is rewriting your import calls over to require calls in the bundled code. Am I correct in this assumption?

AdelNorberg commented 3 years ago

I use this package in two environments. In one of them, this package works. There Rollup. In another, where there is nothing but ts (config on screenshots), an error occurs, which is in question in the issue

Did you make sure to add it back to the bitecs package.json? Are you using a bundle like webpack or rollup?

Yeah. I removed node_modules so that there were no problems after changing package.json in node_modules

SupremeTechnopriest commented 3 years ago

can you add "module": "ESNext" to your tsconfig's compilerOptions and try again?

AdelNorberg commented 3 years ago

can you add "module": "ESNext" to your tsconfig's compilerOptions and try again?

image

With ESnext in ts config, other packages already stop working with the same error

AdelNorberg commented 3 years ago

If you look at other packages (three, uWebSockets.js) that work for me, then they do not have in package.json type: "module"

hazulu commented 3 years ago

I am having a similar issue as well-- I created a new TS project and tried using the library and was receiving the same error.

I was able to fix the issue if added type: module to my app's package.json, and compiled tsc rather than trying to run it. I ran the build with the following TSConfig: image and this is the package.json: image (I did not run npm run build, I just used the tsc command)

mrchantey commented 3 years ago

same here, removing "type": "module" from the bitECS package.json did the trick (i'm using typescript+webpack) ps awesome library! :)

NateTheGreatt commented 3 years ago

the transition from commonjs to es modules has been quite a mess haha. just fyi i haven't forgotten about this, still trying to figure out what the best solution is

EnderShadow8 commented 3 years ago

I'm pretty sure there's a way to configure the package.json to provide different entry points depending on whether the module was required or imported, but I'm still figuring out the specfics of how that would work with TS and stuff

Best policy is to just use ESM since ESM can import CJS but not the other way around.

EnderShadow8 commented 3 years ago

CJS can import ESM as follows:

const importedModule = await import("./myModule.mjs")

Hope this solves the problem.