When I try to start my app on Heroku I got the following stack trace. It is just a basic ts.app like you see with ts-node and nodemon. If you need any more Infos just tell me that because it's my first post. I am really interested in what the answer is going to be.
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
This is my 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"
}
}
And this is my tsconfig
{
"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"]
}
I changed my imports from:
import blah from './modules/blah'
to
import blah from './modules.blah.js'
Ran into this if I'm importing typescript code from a react library but for a backend script, in this case mocha tests. Its expecting .ts files but finding .tsx files I think.
In my case I had to move some functions from the .tsx react files into the .ts backend code I was working with.
I was trying to run both js and ts files on the same package with "type": "module" set using es6 syntax for imports. Following mysterycommand's answer, installing ts-node package and running ts files with node --loader ts-node/esm worked.
"type": "module" from package.json if it's addedtsconfig.json under the compilerOptions Set module property to CommonJS module: "CommonJS" and moduleResolution: "Node"module: "ESNext"1- Add "type": "module" to package.json
2- Install ts-node npm i -g ts-node
3- Go to tsconfig.json and add the following:
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node",
/* ... your props ... */
},
"ts-node": {
"esm": true
}
}
4- Run ts-node fileName.ts
MARCH 2022
USING: Node 16.6.2, ts-node v 10.7.0
What worked for me was having "type": "module" in package.json, and adding
node --experimental-specifier-resolution=node --loader ts-node/esm ./src/app.ts
tsconfig.json:
{
"compilerOptions": {
"module": "ESNext",
"esModuleInterop": true,
"target": "ESNext",
"moduleResolution": "Node",
"outDir": "dist",
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"isolatedModules": false,
"strict": true,
"noImplicitAny": true,
"useUnknownInCatchVariables": false,
"inlineSourceMap": true
},
"ts-node": {
"esm": true
},
"lib": ["esnext"]
}
Credits to @FelipePlets for the useful answer here
EDIT
You may want to use a non-esnext option, as per the ts docs:
The special ESNext value refers to the highest version your version of TypeScript supports. This setting should be used with caution, since it doesn’t mean the same thing between different TypeScript versions and can make upgrades less predictable.
changing my
"moduleResolution": "node",
to
"moduleResolution": "Node",
in package.json solved this for me
Update: Thanks to some maintainers, a working solution is now officially documented by webpack. Just take a small look at the official webpack documentation.
The second solution is similar to other answers here and works perfectly fine in my case.
In your package.json {Module:commonJS}, and tsconfig.json {module:commonJS},to ts-node youname.ts
Add this to tsconfig.json
{
/* ... your props ... */
"ts-node": {
"compilerOptions": {
"module": "CommonJS"
}
}
}
before this:
node --loader ts-node/esm ./my-script.ts
I had to update ssri
npm update ssri --depth 5
I first came across this problem probably over a year ago, and ts-node has yet to fix it. None of the above solutions worked for me, and I have seemingly tried everything.
I just resorted to using tsc --outDir out file.ts and then running the file normally with node out/file.js, and then adding out to the .gitignore.
The thought behind ts-node is wonderful, just really sucks when it can't handle seemingly straightforward examples like this. Apologies the solution doesn't use ts-node, but I couldn't get it to work.
Not sure if this will help anyone but I fixed it by putting this at the start:
#!/usr/bin/env node
Removing "type": "module" from package.json and adding:
"compilerOptions": {
"module": "CommonJS"
},
In tsconfig.json fixed this for me.
I followed advice given herein. I also had to uninstall lodash-es and install lodash instead to make it work.
use
node --loader ts-node/esm ./my-script.ts
instead of
ts-node ./my-script.ts
Remove "type": "module" from package.json
https://github.com/TypeStrong/ts-node/issues/935
If you don't want to remove "type": "module" (for example if you're using import statements in your .ts which allows the inference of types from modules), then you can use the following option in tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true,
}
}
And then you can start the server with the config using ts-node.
Install:
npm install -g ts-node
Run:
ts-node my_server.ts