简体   繁体   中英

SyntaxError: The requested module 'sqlite' does not provide an export named 'default'

i am having trouble when trying to import sqlite, I add the following line:

import sqlite from 'sqlite';

and I get the following error

file:///D:/WebPro/WebProg/cwCode/dbInteract.js:2
import sqlite from 'sqlite';
       ^^^^^^
SyntaxError: The requested module 'sqlite' does not provide an export named 'default'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:105:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:151:5)
    at async Loader.import (node:internal/modules/esm/loader:166:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
npm ERR! code 1
npm ERR! path D:\WebPro\WebProg\cwCode
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node svr.js

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Rory\AppData\Local\npm-cache\_logs\2021-05-04T13_37_27_066Z-debug.log
PS D:\WebPro\WebProg\cwCode>

I did not have this problem before I changed the type in the package.json to 'module'

If I then change is to

import sqlite from 'sqlite3';

i get this error instead

file:///D:/WebPro/WebProg/cwCode/svr.js:4
const express = require('express');
                ^

ReferenceError: require is not defined
    at file:///D:/WebPro/WebProg/cwCode/svr.js:4:17
    at ModuleJob.run (node:internal/modules/esm/module_job:154:23)
    at async Loader.import (node:internal/modules/esm/loader:166:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
npm ERR! code 1
npm ERR! path D:\WebPro\WebProg\cwCode
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node svr.js

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Rory\AppData\Local\npm-cache\_logs\2021-05-04T13_44_05_322Z-debug.log
PS D:\WebPro\WebProg\cwCode> 

With ESM modules, there are two ways to export functionnalities: either as default exports, or as named exports. The way you import these functionnalities depends on how they are actually exported in the first place.

import defaultExport from 'module1'                  // Import default export 
import { namedExport1, namedExport2 } from 'module2' // Import named exports

Note a module may actually have both a default and named exports, or just either one of them.

Even though they are libraries for the same database, the sqlite and sqlite3 packages apparently just export their apis in different ways - and sqlite more specifically doesn't seem to have a default export.

Also, when you set your package.json type key to module , you instruct node to load you program with the ESM module system. This system doesn't define a require() function - which is defined by the other node.js mainstream module system called CommonJS. You should just stick to the import syntax here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM