嘗試使用以下命令使用 NodeJS 編譯打字稿代碼時:
npx ts-node src/server.ts
我收到以下錯誤:
SyntaxError: Cannot use import statement outside a module
我按照錯誤建議的說明進行操作:
警告:要加載 ES 模塊,請在 package.json 中設定 "type": "module" 或使用 .mjs 擴展名。
但是在遵循這些之后,我仍然沒有運氣,而是拋出了另一個錯誤。
這是我的 server.ts 檔案的內容。
import App from './app';
const app = new App();
app.listen(3080);
tsconfig.json 內容:
{
"compileOnSave": false,
"compilerOptions": {
"target": "ES2017",
"lib": ["es2017"],
"typeRoots": ["node_modules/@types"],
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"pretty": true,
"sourceMap": true,
"declaration": true,
"outDir": "./dist",
"allowJs": true,
"noEmit": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"importHelpers": true,
"baseUrl": "src",
"skipLibCheck": true,
"paths": {
"@/*": ["*"],
}
},
"include": ["src/**/*.ts", "src/**/*.json", ".env"],
"exclude": ["node_modules"]
}
我不太確定是什么導致了這種情況,但非常感謝在這個問題上的一些智慧。
理想情況下,我想通過 server.ts 檔案加載應用程式,同時維護 TypeScript 內容。
uj5u.com熱心網友回復:
您可能需要"moduleResolution": "node"在compilerOptionsfor ts 中識別這些型別的匯入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363250.html
