Can you help me understand this behavior and share how would you solve this “properly”?
I’m running Node.JS (v20.12.2), Yarn (4.1.1), tsc (5.4.5) and have following goals:
- Run the app locally with ts-node & ts-node-dev.
- Export the app with tsc to run in elsewhere.
I can run (2) yarn build (basically yarn tsc), then node dist/server.js works.
I fail at (1) yarn dev / start (yarn ts-node ./src/index.ts) both fail with the error below. If I do what it says and add ./src folder as dependency in package.json it works, but that looks very strange to me… I don’t get why tsc is able to work with the src folder and ts-node is not. Is that really how this is supposed to work or I miss something obvious? Thanks!
File structure:
- project home
- package.json
- .pnp.cjs
- tsconfig.json
- src
- server.ts
- dist
- server.js
Error:
$ yarn ts-node-dev --exit-child src/server.ts
<project-root>/.pnp.cjs:7812
return Object.defineProperties(new Error(message), {
^
Error: Your application tried to access src, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.
Required package: src (via "src/server.ts")
Required by: <project-root>/
at makeError (<project-root>/.pnp.cjs:7812:34)
at resolveToUnqualified (<project-root>/.pnp.cjs:9450:21)
at resolveRequest (<project-root>/.pnp.cjs:9587:31)
at <project-root>/api/.pnp.cjs:9650:26
at resolveRequest (<project-root>/.yarn/__virtual__/ts-node-dev-virtual-1a2df4f79d/6/.yarn/berry/cache/ts-node-dev-npm-2.0.0-a9d487396c-10c0.zip/node_modules/ts-node-dev/lib/resolveMain.js:11:16)
at exports.resolveMain <project-root>/.yarn/__virtual__/ts-node-dev-virtual-1a2df4f79d/6/.yarn/berry/cache/ts-node-dev-npm-2.0.0-a9d487396c-10c0.zip/node_modules/ts-node-dev/lib/resolveMain.js:30:20)
at exports.runDev (<project-root>/.yarn/__virtual__/ts-node-dev-virtual-1a2df4f79d/6/.yarn/berry/cache/ts-node-dev-npm-2.0.0-a9d487396c-10c0.zip/node_modules/ts-node-dev/lib/index.js:53:30)
at Object.<anonymous> (<project-root>/.yarn/__virtual__/ts-node-dev-virtual-1a2df4f79d/6/.yarn/berry/cache/ts-node-dev-npm-2.0.0-a9d487396c-10c0.zip/node_modules/ts-node-dev/lib/bin.js:136:4)
at Module._compile (node:internal/modules/cjs/loader:1369:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
package.json:
{
"name": "test-app",
"packageManager": "[email protected]",
"version": "0.1.0",
"description": "test-app",
"scripts": {
"dev": "ts-node-dev --exit-child ./src/index.ts",
"start": "ts-node ./src/index.ts",
"build": "tsc"
},
"devDependencies": {
"@tsconfig/node-lts": "^20.1.3",
"@types/express": "^4",
"@types/node": "^20.12.7",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typescript": "^5.4.5"
},
"dependencies": {
"express": "^4.19.2",
"graphql": "^16.8.1",
"graphql-http": "^1.22.1"
}
}
tsconfig.json:
{
"extends": "@tsconfig/node-lts/tsconfig.json",
"include": ["src"],
"compilerOptions": {
/* Emit */
"outDir": "dist",
}
}



