我有一个用typescript编写的node服务器,使用调试器和vs代码运行良好。
....
import { logIn, getLogins, findUser } from "./src/logins";
const app = express ();
app.use(express.json());
....
....
字符串
但是当我尝试运行node dist\app.js
时,
SyntaxError: Cannot use import statement outside a module
型
因此,according to this post,我将"type": "module",
添加到package.json
。但现在我得到错误
[ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\quilk\source\repos\ridehub-server\dist\src\logins' imported from C:\Users\quilk\source\repos\ridehub-server\dist\app.js
型
请帮帮忙!package.json:
{
"name": "ridehub-server",
"version": "1.0.0",
"description": "learning",
"main": "dist\\app.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.ts",
"dev": "tsx ..."
},
"repository": {
"type": "git",
"url": "quilkin\\ridehub-server"
},
"author": "quilkin",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"linq": "^4.0.2",
"method-override": "^3.0.0",
"mysql": "^2.18.1",
"nodemailer": "^6.9.7",
"xml2js": "^0.6.2"
},
"devDependencies": {
"@types/cors": "^2.8.15",
"@types/express": "^4.17.20",
"@types/node": "^20.8.8",
"tsx": "^3.14.0",
"typescript": "^5.2.2"
}
}
型
tsconfig.json:
{
"compilerOptions": {
"composite": true,
"sourceMap": true,
"baseUrl": ".",
"types": ["node"],
"paths": {
"@/*": ["./src/*"]
},
"outDir": "dist",
"allowSyntheticDefaultImports": true,
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"target": "ES2020",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"references": [
{
"path": "../ridehub-common",
}
]
}
型
1条答案
按热度按时间hs1rzwqc1#
我找到了答案here.
问题在于为导入命名文件扩展名:
字符串
添加
.ts
扩展名(逻辑扩展名,因为这是我导入的文件的名称)给出错误型
实际上需要的是添加一个
.js
扩展。型
这对我来说似乎完全不合逻辑-在该文件夹中没有这样的文件,它只在运行'tsc'后在
dist
文件夹中创建。哦,你不能使用
@/logins.js
,你必须使用./src/logins.js
或其他什么。