亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤 [ERR_UNSUPPORTED_DIR_IMPORT]:嘗試在本地啟動 Nodej

錯誤 [ERR_UNSUPPORTED_DIR_IMPORT]:嘗試在本地啟動 Nodej

皈依舞 2023-05-25 16:43:24
我在嘗試將我的應用程序部署到 Heroku 時陷入了一個循環。import cors from 'cors'由于“無法在 Common JS 中加載 ES6 模塊”錯誤,我的導入語句(例如)似乎阻止了應用程序在生產環境中啟動。在本地它運行得很好。"type": "module"但是,當我嘗試通過添加到我的錯誤來解決上述錯誤時,package.json我得到了一組全新的錯誤,應用程序將不再在本地運行。我相信這個錯誤是由于我初始化 sequelize 和相關模型的方式造成的,但我不確定。我想解決這個錯誤,但需要幫助導入新語法……我想。錯誤,package.json并index.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...
查看完整描述

5 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

明確地指向您的主文件(通常是index.js)。例如

import?...?from?'./models'??????????//??
import?...?from?'./models/index.js'?//??

替代方式(請參閱下面的更新):

使用--experimental-specifier-resolution=node標志。例如:

node?--experimental-specifier-resolution=node?main.js

更新(Node.js v19+):

Node.js 已刪除--experimental-specifier-resolution標志。它的功能現在可以通過自定義加載器來實現。


查看完整回答
反對 回復 2023-05-25
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

嘗試將--experimental-specifier-resolution=node標志添加到您的start腳本中package.json,特別是如果您在本地運行時使用此標志 - 這是 Heroku 將用來啟動您的服務器的腳本。

scripts": {
    "start": "node --experimental-specifier-resolution=node index",
    ...
},


查看完整回答
反對 回復 2023-05-25
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

在 Node.js 中,import 語句只允許在 ES 模塊中使用。因此,目錄導入在 Node.js 中不起作用。


查看完整回答
反對 回復 2023-05-25
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

嘗試添加此代碼package.json

"scripts": {  
    "start": "node --experimental-specifier-resolution=node src/index"
    }


查看完整回答
反對 回復 2023-05-25
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

選項 1 "type": "module", 應該在 "main" 之前:

選項:2 將源文件更新為 .mjs 擴展名


查看完整回答
反對 回復 2023-05-25
  • 5 回答
  • 0 關注
  • 1013 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號