Currently, I am testing LevelDB as a key–value store for my Node.js projects, but I am having this issue: when I import Level and run the sample code from the website, leveljs.org, I get this error:
TypeError: level is not a function
at Object.<anonymous> (/home/runner/level/index.js:2:12)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
I have searched for this error and nothing has came up. I have tried importing Level
in different ways and none of them have worked. Here is my code, which is identical to the sample code on the website:
const level = require('level')
const db = level('./db', { valueEncoding: 'json' })
db.put('key', { example: true }, function (err) {
if (err) throw err
db.get('key', function (err, value) {
if (err) throw err
console.log(value)
})
})
When in doubt, always check the documentation at GitHub:
const { Level } = require('level') // Create a database const db = new Level('example', { valueEncoding: 'json' })
The README also has this note:
If you are upgrading: please see UPGRADING.md.
Version 8.0.0 (released 2022-03-25) states:
Changes to initialization
We started using classes, which means using
new
is now required. If you previously did:const level = require('level') const db = level('db')
You must now do:
const { Level } = require('level') const db = new Level('db')
It also notes:
This release replaces
leveldown
andlevel-js
withclassic-level
andbrowser-level
.
The changelog lists this as a breaking change:
Breaking: switch to classic-level and browser-level (#215) (
ad22b21
) (Vincent Weevers).
The website is outdated (as of 2022-12-22, 22:09:03 UTC), and below the code sample it says:
As of
level@5
, the above code works in Node.js, Electron and browsers!
But you’re almost certainly using 8.0.0, not 5.0.0.
Shortly after bringing it up on the repository for leveljs.org, they fixed it.