typicode / lowdb

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

SyntaxError: Cannot use import statement outside a module #470

Closed StupidRepo closed 3 years ago

StupidRepo commented 3 years ago

So I have this at the top of my script:

require('dotenv').config()

import { join } from 'path'
import { Low, JSONFile } from 'lowdb'

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

const Discord = require('discord.js')
const bot = new Discord.Client()

but I keep getting SyntaxError: Cannot use import statement outside a module and using const { blah blah } = require("lowdb") says Must use import to load ES Module: /Users/username/WebstormProjects/projectname/node_modules/lowdb/lib/index.js.

Please help 👍

StupidRepo commented 3 years ago

Turns out I can change the file extension to .mjs but then I can't use require() so what do I do?

typicode commented 3 years ago

You can change require('dotenv') to import ... https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import

This should do the trick :)

You can also set "type": "module" in your package.json and keep .js as well.