[英]ts-node TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
[英]SyntaxError: Cannot use import statment without a module, TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Catch 22
我一直有節點js的問題。
這是相關代碼( app.ts
):
const express = require("express");
const auth = require("./service/auth");
import { Request, Response } from "express";
import User from "./model/User";
import con from "./config/db";
const port = 3000;
const app = express();
app.use(express.json());
...
我得到的錯誤是:
(node:16524) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\010bo\OneDrive\Documents\GitHub\rpg-backend\app\app.ts:4
import { Request, Response } from "express";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1117:16)
at Module._compile (internal/modules/cjs/loader.js:1165:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
所以我尋找解決方案並發現: SyntaxError: Cannot use import statement outside a module
這告訴我:
將 "type": "module" 添加到最近的父 package.json
所以將"type": "module"
添加到package.json。 我現在得到的 output 是:
internal/modules/run_main.js:54
internalBinding('errors').triggerUncaughtException(
^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\010bo\OneDrive\Documents\GitHub\rpg-backend\app\app.ts
[90m at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)[39m
[90m at Loader.getFormat (internal/modules/esm/loader.js:98:42)[39m
[90m at Loader.getModuleJob (internal/modules/esm/loader.js:227:31)[39m
[90m at async Loader.import (internal/modules/esm/loader.js:161:17)[39m {
code: [32m'ERR_UNKNOWN_FILE_EXTENSION'[39m
}
所以我再次在線 go,尋找解決方案並發現: 無法運行我的 Node.js Typescript 項目類型錯誤 [ERR_UNKNOWN_tsFILE.
這告訴我:
從 package.json 中刪除“類型”:“模塊”
所以我想我已經發現自己處於第 22 條問題的境地。 我不確定接下來要去哪里 go。
我還嘗試了SyntaxError 中的其他解決方案:無法在模塊外使用導入語句
使用 .mjs 擴展名顯式命名文件。 所有其他文件,例如 .js 將被解釋為 CommonJS,如果 package.json 中未定義類型,則這是默認值。
這不起作用,要么給我同樣的錯誤,要么給我不同的語法錯誤,我認為是因為我使用的是 typescript。
提前感謝任何可以提供幫助的人。
const { Request, Response } = require("express")
其他的也一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.