Can';t运行my Node.js Typescript项目TypeError[错误未知文件扩展名]:未知文件扩展名;。ts";对于/app/src/app.ts
当我尝试在Heroku上启动我的应用程序时,我得到了以下堆栈跟踪。 它只是一个基本的ts.app,就像你看到的ts node和nodemon一样。 如果你需要更多的信息,请告诉我,因为这是我的第一篇文章。 我真的对答案很感兴趣Can';t运行my Node.js Typescript项目TypeError[错误未知文件扩展名]:未知文件扩展名;。ts";对于/app/src/app.ts,node.js,typescript,npm,Node.js,Typescript,Npm,当我尝试在Heroku上启动我的应用程序时,我得到了以下堆栈跟踪。 它只是一个基本的ts.app,就像你看到的ts node和nodemon一样。 如果你需要更多的信息,请告诉我,因为这是我的第一篇文章。 我真的对答案很感兴趣 2020-05-30T00:03:12.201106+00:00 heroku[web.1]: Starting process with command `npm start` 2020-05-30T00:03:14.405285+00:00 app[web.1]:
2020-05-30T00:03:12.201106+00:00 heroku[web.1]: Starting process with command `npm start`
2020-05-30T00:03:14.405285+00:00 app[web.1]:
2020-05-30T00:03:14.405303+00:00 app[web.1]: > discordtoornamentmanager@1.0.0 start /app
2020-05-30T00:03:14.405303+00:00 app[web.1]: > ts-node src/App.ts
2020-05-30T00:03:14.405304+00:00 app[web.1]:
2020-05-30T00:03:14.833655+00:00 app[web.1]: (node:23) ExperimentalWarning: The ESM module loader is experimental.
2020-05-30T00:03:14.839311+00:00 app[web.1]: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts
2020-05-30T00:03:14.839312+00:00 app[web.1]: at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
2020-05-30T00:03:14.839314+00:00 app[web.1]: at Loader.getFormat (internal/modules/esm/loader.js:113:42)
2020-05-30T00:03:14.839315+00:00 app[web.1]: at Loader.getModuleJob (internal/modules/esm/loader.js:244:31)
2020-05-30T00:03:14.839315+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-05-30T00:03:14.839316+00:00 app[web.1]: at Loader.import (internal/modules/esm/loader.js:178:17)
2020-05-30T00:03:14.847801+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-05-30T00:03:14.847998+00:00 app[web.1]: npm ERR! errno 1
2020-05-30T00:03:14.848957+00:00 app[web.1]: npm ERR! discordtoornamentmanager@1.0.0 start: `ts-node src/App.ts`
2020-05-30T00:03:14.849050+00:00 app[web.1]: npm ERR! Exit status 1
2020-05-30T00:03:14.849172+00:00 app[web.1]: npm ERR!
2020-05-30T00:03:14.849254+00:00 app[web.1]: npm ERR! Failed at the discordtoornamentmanager@1.0.0 start script.
2020-05-30T00:03:14.849337+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-05-30T00:03:14.854859+00:00 app[web.1]:
2020-05-30T00:03:14.854998+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-05-30T00:03:14.855069+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-05-30T00_03_14_850Z-debug.log
2020-05-30T00:03:14.907689+00:00 heroku[web.1]: Process exited with status 1
2020-05-30T00:03:14.943718+00:00 heroku[web.1]: State changed from starting to crashed
这是我的package.json
{
"name": "discordtoornamentmanager",
"version": "1.0.0",
"description": "",
"main": "dist/app.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon -x ts-node src/App.ts",
"start": "ts-node src/App.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^14.0.5",
"axios": "^0.19.2",
"discord.js": "^12.2.0",
"pg": "^8.2.1",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.25",
"typescript": "^3.9.3",
"nodemon": "^2.0.4",
"ts-node": "8.10.1"
}
}
这是我的tConfig
{
"compilerOptions": {
"lib": [
"es6"
],
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"sourceMap": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
从package.json中删除“type”:“module”
{
"name": "discordtoornamentmanager",
"version": "1.0.0",
"description": "",
"main": "dist/app.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon -x ts-node src/App.ts",
"start": "ts-node src/App.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^14.0.5",
"axios": "^0.19.2",
"discord.js": "^12.2.0",
"pg": "^8.2.1",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.25",
"typescript": "^3.9.3",
"nodemon": "^2.0.4",
"ts-node": "8.10.1"
}
}
如果您不想删除
“type”:“module”
(例如,如果您在.ts中使用import
语句,允许从模块推断类型),则可以在tsconfig.json
中使用以下选项:
{
"compilerOptions": {
"esModuleInterop": true,
}
}
然后,您可以使用ts node
使用配置启动服务器
安装:
npm install -g ts-node
运行:
使用
而不是
ts-node ./my-script.ts
在此之前:
node——加载程序ts node/esm./my script.ts
我必须更新ssri
npm update ssri--depth 5
似乎es模块和ts节点存在问题。这是esm支持反馈的另一个问题。您可以从使用部分尝试解决方法。另外,ESM模块加载器是实验性的
,所以它当然是不稳定的方法。删除“type”:“module”
让我加载ES模块,在包中设置“type”:“module”。json
对于我在中使用的脚本,导入{…}表单“../
。@DanDascalescu,你说得对。不幸的是,如果没有“type”:“module”
,则无法使用ESM。所描述的解决方案之所以有效,是因为作者在他的tsconfig.json:)中提到:“模块”:“commonjs”
)否则,您可以尝试跳入“我来自答案”,现在我处于无限状态loop@wormsparty确切地我还在IT中什么是ts节点/esm?这里没有更多信息的描述,这是正确的答案。如果我想使用模块,删除types=module
不是解决方案。一个解决方法是脚本调用中的tsconfig模块选项将被commonjs覆盖。但这也令人不快。要使用nodemon运行它,我们可以使用环境变量。然后调用可能看起来像这样:NODE\u OPTIONS='--没有警告--loader ts NODE/esm'nodemon--观看'src/***'-e ts,tsx--exec ts NODE./server.ts
很多人都面临这个问题
ts-node ./my-script.ts