So I was following a NodeJS tutorial.
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json')
const db = low(adapter)
I tried using this:
import {default as lowdb} from 'lowdb'
This:
import lowdb from 'lowdb'
It all throws errors.
Am a bit confused on how to use it in the new way of import
than the require
which is depreciated.
There is one thing to change and a second thing to check if that first one doesn't quite solve it.
The module does not have a default export, and the two import
statements you tried assume there is a default export. Follow along with the usage example in the package page, which includes import { Low, JSONFile } from 'lowdb'
. Using that and/or other things listed in the API at the package page should get you what you need.
If you do that and it still doesn't work, be sure that you have either named your file .mjs
or else included "type": "module"
in your package.json
. See the Node.js documentation for packages for more information. (This will work for all Node.js versions supported as of this writing, which would be Node.js versions 12.x, 14.x, and 16.x. If you are running 10.x or earlier, you will need to upgrade.)