Search code examples
node.jstypescript

Create a basic server using NodeJS , Express, TypeScript and do the followinng in seperate apis respectively in same app.ts


PS E:\Nodejs Training\Project_1> npm run build

[email protected] build tsc

PS E:\Nodejs Training\Project_1> npm run start

[email protected] start node dist/node app.js

node:internal/modules/cjs/loader:1147 throw err; ^

Error: Cannot find module 'E:\Nodejs Training\Project_1\dist\node' at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15) at Module._load (node:internal/modules/cjs/loader:985:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) at node:internal/main/run_main_module:28:49 { code: 'MODULE_NOT_FOUND', requireStack: [] }

Node.js v20.11.1

package.json

{
"name": "project_1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
    "start": "node dist/node app.js",
    "build": "tsc"
},
...}

Tried running the code from different directory not Working and also tried many different things also.


Solution

  • I saw this in your package.json, Basically you are telling node to find something node which is inside dist/ (but it doesn't exist).

    {
       ...
       "start": "node dist/node app.js"
       ...
    }
    

    When you run npm run build, It builds and puts the output into the ./dist directory, right? Then it should be,

    {
       ...
       "start": "node ./dist/app.js"
       ...
    }