typicode / lowdb

Simple and fast JSON database
MIT License
21.49k stars 923 forks source link

[ERROR] Must use import to load ES Module #537

Closed angelhdzmultimedia closed 2 years ago

angelhdzmultimedia commented 2 years ago

Error

C:\Users\AngelHdz\Documents\Development\JavaScript\projects\lowdb>npm run dev

> lowdb@1.0.0 dev
> ts-node-dev --respawn src/main.ts

[INFO] 22:54:30 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.8.0, typescript ver. 4.7.2)
Compilation error in C:\Users\AngelHdz\Documents\Development\JavaScript\projects\lowdb\src\main.ts
Error: Must use import to load ES Module: C:\Users\AngelHdz\Documents\Development\JavaScript\projects\lowdb\src\main.ts
    at Object.<anonymous> (C:\Users\AngelHdz\Documents\Development\JavaScript\projects\lowdb\src\main.ts:1:7)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._compile (C:\Users\AngelHdz\Documents\Development\JavaScript\projects\lowdb\node_modules\source-map-support\source-map-support.js:568:25)
    at Module.m._compile (C:\Users\AngelHdz\AppData\Local\Temp\ts-node-dev-hook-4245052384327548.js:69:33)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)        
    at require.extensions..jsx.require.extensions..js (C:\Users\AngelHdz\AppData\Local\Temp\ts-node-dev-hook-4245052384327548.js:114:20)
    at require.extensions.<computed> (C:\Users\AngelHdz\AppData\Local\Temp\ts-node-dev-hook-4245052384327548.js:71:20)
    at Object.nodeDevHook [as .ts] (C:\Users\AngelHdz\Documents\Development\JavaScript\projects\lowdb\node_modules\ts-node-dev\lib\hook.js:63:13)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
[ERROR] 22:54:30 Error: Must use import to load ES Module: C:\Users\AngelHdz\Documents\Development\JavaScript\projects\lowdb\src\main.ts

main.ts

console.log('Test');

import {Low, JSONFile} from 'lowdb'
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url))

const file = join(__dirname, 'db.json')
const adapter = new JSONFile(file)
const db = new Low(adapter)

async function main() {
    await db.read()
}

main()

package.json

{
  "type": "module",
  "name": "lowdb",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "ts-node-dev --respawn src/main.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/lowdb": "^1.0.11",
    "@types/node": "^17.0.36",
    "lowdb": "^3.0.0",
    "ts-node-dev": "^2.0.0",
    "typescript": "^4.7.2"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "esnext",
        "target": "esnext",
        "moduleResolution": "node",
        "esModuleInterop": true
    }
}
angelhdzmultimedia commented 2 years ago

Update

I tried making a Nest.js application with TypeScript, as it includes a lot of packages and configuration ready for a TypeScript/Node environment.

And managed to get Lowdb working there.

Now I'm going to compare both tsconfig.json to see what changed, and if apart from ts-node-dev, I need any other package.