Typescript - 警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名
Posted
技术标签:
【中文标题】Typescript - 警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名【英文标题】:Typescript - Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension 【发布时间】:2021-09-11 12:26:26 【问题描述】:我用 nodejs 和 typescript 运行一个应用程序。这是我的第一步。当我想在 heroku 中部署后端应用程序时,出现错误(本地主机中没有错误):
警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名。
当然我已经设置了“type”:“module”,但是这次我有这个错误:
[ERR_UNKNOWN_FILE_EXTENSION]:未知文件扩展名“.ts”
我已经尝试了在与这些错误相关的不同线程中提出的多种解决方案
To load an ES module, set "type": "module" in the package.json or use the .mjs extension Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts To load an ES module, set "type": "module" in the package.json or use the .mjs extension Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts不幸的是,它们都不起作用(提醒,当我不使用 type:module 时,它在本地工作)
节点版本:v14.15.4
这里是我的 tsconfig.json
"compilerOptions":
/* Basic Options */
"target": "ESNext",
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"strict": true, /* Enable all strict type-checking options. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
,
"include" : [
"src/**/*.ts" /* Include every ts file in source folder */
, "src/server..mjs"],
"exclude" : [
"node_modules" /* exclude everything in node_modules */
],
这是我的 package.json
"name": "backend-typescript",
"version": "1.0.0",
"description": "",
"main": "src/server.ts",
"scripts":
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/server.ts",
"dev": "nodemon src/server.ts"
,
"keywords": [],
"author": "",
"license": "ISC",
"dependencies":
"@types/body-parser": "^1.19.0",
"@types/cors": "^2.8.10",
"@types/express": "^4.17.12",
"@types/mongoose": "^5.11.97",
"@types/node": "^15.12.4",
"axios": "^0.21.1",
"body-parser": "^1.19.0",
"config": "^3.3.6",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"googleapis": "^78.0.0",
"http-status-codes": "^2.1.4",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"nodemon": "^2.0.7",
"passport": "^0.4.1",
"passport-google-oauth20": "^2.0.0",
"throat": "^6.0.1",
"typescript": "^4.3.4"
,
"devDependencies":
"@types/config": "^0.0.38",
"@types/jsonwebtoken": "^8.5.2",
"@types/passport": "^1.0.6",
"@types/passport-google-oauth20": "^2.0.8"
【问题讨论】:
【参考方案1】:通过npm安装ts-node
,使用控制台运行ts-node nameoffile.ts
。
【讨论】:
以上是关于Typescript - 警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名的主要内容,如果未能解决你的问题,请参考以下文章
如何在没有模块加载系统的情况下将我的 Typescript 编译成单个 JS 文件?
TypeScript 找不到模块 flatpickr,即使它包含 typescript 类型