我有一个可以使用Node.js和PostgreSQL的api。从终端运行 node index.js 时,将代码转换为ES6时出现以下错误。
node:internal/process/esm_loader:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\node\api\queries' imported from C:\node\api\index.jss
Did you mean to import ../queries.js?
at new NodeError (node:internal/errors:258:15)
at finalizeResolution (node:internal/modules/esm/resolve:276:11)
at moduleResolve (node:internal/modules/esm/resolve:699:10)
at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:810:11)
at Loader.resolve (node:internal/modules/esm/loader:85:40)
at Loader.getModuleJob (node:internal/modules/esm/loader:229:28)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:51:40)
at link (node:internal/modules/esm/module_job:50:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
我有三个文件的行已更改:
index.js
const express = require('express')
const bodyParser = require('body-parser')
const db = require('./queries').default
转换为:
import express from 'express'
import { json, urlencoded } from 'body-parser'
import db from './queries'
queries.js
const Pool = require('pg').Pool
确认为:
import { Pool } from 'pg'
package.json -我添加了:
"type": "module",
我还需要做什么来解决此错误?