diff --git a/doc/api/errors.md b/doc/api/errors.md index 712d73cc140..ed0f06ffa8a 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2548,6 +2548,17 @@ OpenSSL crypto support. An attempt was made to use features that require [ICU][], but Node.js was not compiled with ICU support. + + +### `ERR_NO_SQLITE` + + + +An attempt was made to use [`node:sqlite`][], but Node.js was not compiled with +SQLite support. + ### `ERR_NO_TEMPORAL` @@ -4872,6 +4883,7 @@ An error occurred trying to allocate memory. This should never happen. [`new URL(input)`]: url.md#new-urlinput-base [`new URLPattern(input)`]: url.md#new-urlpatternstring-baseurl-options [`new URLSearchParams(iterable)`]: url.md#new-urlsearchparamsiterable +[`node:sqlite`]: sqlite.md [`package.json`]: packages.md#nodejs-packagejson-field-definitions [`postMessage()`]: worker_threads.md#portpostmessagevalue-transferlist [`postMessageToThread()`]: worker_threads.md#worker_threadspostmessagetothreadthreadid-value-transferlist-timeout diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 78d3de196a5..28f7409ec7f 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1650,6 +1650,8 @@ E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support', Error); E('ERR_NO_ICU', '%s is not supported on Node.js compiled without ICU', TypeError); +E('ERR_NO_SQLITE', + 'Node.js is not compiled with SQLite support', Error); E('ERR_NO_TEMPORAL', 'Temporal is not supported in this environment', Error); E('ERR_NO_TYPESCRIPT', diff --git a/lib/sqlite.js b/lib/sqlite.js index 4934a4b7700..5770583c5e4 100644 --- a/lib/sqlite.js +++ b/lib/sqlite.js @@ -1,5 +1,11 @@ 'use strict'; +const { ERR_NO_SQLITE } = require('internal/errors').codes; + +if (!process.versions.sqlite) { + throw new ERR_NO_SQLITE(); +} + const { Database, Statement, Session, constants, backup } = internalBinding('sqlite'); diff --git a/test/parallel/test-sqlite-without-sqlite.js b/test/parallel/test-sqlite-without-sqlite.js new file mode 100644 index 00000000000..ee1a44e44f8 --- /dev/null +++ b/test/parallel/test-sqlite-without-sqlite.js @@ -0,0 +1,11 @@ +// Flags: --experimental-sqlite +'use strict'; + +const { hasSQLite } = require('../common'); +const assert = require('node:assert'); + +if (hasSQLite) { + assert.strictEqual(typeof require('node:sqlite').Database, 'function'); +} else { + assert.throws(() => require('node:sqlite'), { code: 'ERR_NO_SQLITE' }); +}