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 import statement outside a module / ERR_REQUIRE_ESM #9

Open
gvrancken opened this issue Sep 3, 2021 · 2 comments
Open
Assignees
Labels

Comments

@gvrancken
Copy link

gvrancken commented Sep 3, 2021

(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
Copy link
Owner

fnogatz commented Sep 3, 2021

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]

@fnogatz fnogatz self-assigned this Sep 3, 2021
@murkle
Copy link

murkle commented May 21, 2023

@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

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

No branches or pull requests

3 participants