fnogatz / dgtchess

An event-driven JavaScript connector for electronic DGT chess boards
GNU General Public License v3.0
24 stars 7 forks source link

Cannot use import statement outside a module / ERR_REQUIRE_ESM #9

Open gvrancken opened 3 years ago

gvrancken commented 3 years ago

(using Node 16) When using with node an the supplied example

import Board from 'dgtchess'
const board = new Board('/dev/ttyUSB0')

we get the error: SyntaxError: Cannot use import statement outside a module

If we add "type": "module" to our package.json, we then get the error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

How can we fix this?

fnogatz commented 3 years ago

The import statement works only if your code is an ES module. This can be done either by putting the "type": "module" in your package.json, or by using the .mjs file extension instead of .js.

Example of a minimal working example: ``` > head package.json test.js ==> package.json <== { "type": "module", "dependencies": { "dgtchess": "^1.0.0" } } ==> test.js <== import Board from 'dgtchess' console.log(Board) > node test.js [class Board extends Board] ```
murkle commented 1 year ago

@gvrancken I've made a simplified version that doesn't use modules (as I don't understand them 😆 ) https://github.com/murkle/dgtchess/blob/main/README.md