Skip to content

Error when importing library #1

@god-s-perfect-idiot

Description

@god-s-perfect-idiot
import { check, iterator, children, anagram, _DAWG } from './scrabbleDict';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.require.extensions.<computed> [as .ts] 
node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous>

This happens in both JS (module and commonjs) and even when using ts.

This is what Cursor says:

Your TypeScript conversion and build are complete, but there is a runtime error when running the server with ts-node or node. The error is:
Apply to package.json
;
This is coming from inside the scrabble-dict package, which is trying to import a TypeScript file (./scrabbleDict) using an ES module import, but Node.js is running in CommonJS mode and/or is not set up to handle .ts files in node_modules.
Why this happens
The scrabble-dict package is not properly compiled for Node.js consumption. It ships TypeScript source files and uses ES module syntax, but your project (and most Node.js projects) expect CommonJS and compiled JavaScript in node_modules.
This is a packaging issue with scrabble-dict and cannot be fixed from your codebase alone.
Solutions
1. Use a different Scrabble dictionary package
Look for a package that is published as JavaScript (not TypeScript source) and works in Node.js.
2. Patch or locally build scrabble-dict
Clone the scrabble-dict repo, build it to JavaScript, and use the built output in your project.
Or, use a fork that is properly published.
3. Use a prebuilt word list (as before)
If you want a quick fix, revert to using a JSON or text word list as you did previously.
What you should do next
Recommended:
If you want to keep using scrabble-dict, you will need to:
Clone the repo
Run npm run build in the scrabble-dict directory to generate JavaScript output
Use the built output in your project (e.g., by copying it to your node_modules or using npm link)
Or, if you want, I can help you revert to the previous (working) JSON-based or another dictionary solution.
Let me know which approach you want:
Try to fix scrabble-dict (instructions provided)
Use a different package (I can suggest one)
Revert to JSON/text-based dictionary
Something else?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions