gpt4 book ai didi

javascript - 错误 [ERR_UNSUPPORTED_DIR_IMPORT] : Directory import when attempting to start Nodejs App locally

转载 作者:行者123 更新时间:2023-12-03 12:14:09 25 4
gpt4 key购买 nike

我在尝试将我的应用程序部署到 Heroku 时陷入了一个循环。由于“无法在 Common JS 中加载 ES6 模块”错误,我的导入语句(例如 import cors from 'cors')似乎阻止了应用程序在生产中启动。在本地它运行得很好。
但是,当我尝试通过添加 "type": "module" 来解决上述错误时给我的package.json我收到一组全新的错误,该应用程序将不再在本地运行。我相信这个错误是由于我初始化 sequelize 和关联模型的方式造成的,但我不确定。我想解决这个错误,但需要帮助导入新的语法......我想。
错误,package.jsonindex.js包括以下。
错误文本

[nodemon] starting `babel-node src/index.js`
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/jeff/Clients/Bummer/Code/Server/src/models' is not supported resolving ES modules imported from /Users/jeff/Clients/Bummer/Code/Server/src/index.js
at finalizeResolution (internal/modules/esm/resolve.js:272:17)
at moduleResolve (internal/modules/esm/resolve.js:699:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
at Loader.resolve (internal/modules/esm/loader.js:85:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:51:40)
at link (internal/modules/esm/module_job.js:50:36) {
code: 'ERR_UNSUPPORTED_DIR_IMPORT',
url: 'file:///Users/jeff/Clients/Bummer/Code/Server/src/models'
}
[nodemon] app crashed - waiting for file changes before starting...
包.JSON
{
"name": "bummer",
"type": "module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node src/index.js",
"dev": "nodemon --exec babel-node src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/node": "^7.8.7",
"@babel/preset-env": "^7.9.6",
"nodemon": "^2.0.4",
"sequelize-cli": "^6.2.0"
},
"dependencies": {
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"pg": "^8.2.1",
"querystring": "^0.2.0",
"request": "^2.88.2",
"sequelize": "^6.3.5",
"sequelize-auto-migrations": "^1.0.3",
"uuid": "^8.0.0"
}
}
索引.js
import cors from 'cors';
import express from 'express';
import models, { sequelize } from './models';
// import routes from './routes';

//Initiaze Express
const app = express();
const routes = require('./routes');


//Helpers for Spotify oAuth
const cookieParser = require('cookie-parser')


// Include Middleware
app.use(express.static(__dirname + '/public'))
.use(cors())
.use(cookieParser())
.use(express.json())
.use(express.urlencoded({ extended: true }))
require('dotenv').config()



// Include all Models
app.use((req, res, next) => {
req.context = {
models,
};
next();
});



// Load Routes from Router Index
app.use('/', routes);

sequelize.sync().then(() => {
app.listen(process.env.PORT, () => {
console.log(`Example app listening on port ${process.env.PORT}!`)
});
});
想法或指针?谢谢!

最佳答案

明确地指向你的主文件(通常是 index.js )。前任:

import ... from './models'          // ❌
import ... from './models/index.js' // ✅

或者:
添加 --experimental-specifier-resolution=nodenode命令。例如:
node --experimental-specifier-resolution=node main.js
看:
https://nodejs.org/api/esm.html#esm_customizing_esm_specifier_resolution_algorithm

关于javascript - 错误 [ERR_UNSUPPORTED_DIR_IMPORT] : Directory import when attempting to start Nodejs App locally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64449464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com