user12479221
user12479221

Reputation:

How to run ts script with package.json?

I try to run typescript files with just a package.json file. So a minimum setup for running typescript files.

I just have a singel file: index.ts:

console.clear();
console.log("nic to see you!!");

and package.json file:


{
  "name": "Todo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}


But if I try to do :

npm start, I will get this error:

Error: Cannot find module 'D:\Mijn Documents\UDEMY\TypeScript\Todo\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\User.USER-PC\AppData\Roaming\npm-cache\_logs\2019-12-24T11_22_12_024Z-debug.log
PS D:\Mijn Documents\UDEMY\TypeScript\Todo>

So my question is: what I have to change that it will compile?

Thank you

oke, I have it now like this:

packagege.json:

{
  "name": "Todo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build":"tsc -p ./src", 
    "start": "npm run build -- -w"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ts-node": "^8.5.4",
    "typescript": "^3.7.4"
  }
}

And I installed typescript

and I do: npm start. I still get this error:

 npm run build -- -w


> [email protected] build D:\Mijn Documents\UDEMY\TypeScript\Todo
> tsc -p ./src "-w"

error TS5057: Cannot find a tsconfig.json file at the specified directory: './src'.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `tsc -p ./src "-w"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\User.USER-PC\AppData\Roaming\npm-cache\_logs\2019-12-24T11_33_38_340Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `npm run build -- -w`
npm ERR! Exit status 1

oke, I have it now like this:

{
  "name": "Todo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsc main.ts dist/",
    "start": "npm run build -- -w"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ts-node": "^8.5.4",
    "typescript": "^3.7.4"
  }
}

and if I do this: npm run. I see this output:


> [email protected] build D:\Mijn Documents\UDEMY\TypeScript\Todo
> tsc main.ts dist/ "-w"

[1:46:58 PM] Starting compilation in watch mode...

error TS6053: File 'dist/.ts' not found.

[1:46:59 PM] Found 1 error. Watching for file changes.

oke, I see now this:

> [email protected] build D:\Mijn Documents\UDEMY\TypeScript\Todo
> tsc -p ./ "-w"

[2:00:31 PM] Starting compilation in watch mode...

[2:00:34 PM] Found 0 errors. Watching for file changes.

But where I can see the results then? Will be nice to go to some port, like: localhost:4200 in the browser, but how to do that?

Upvotes: 5

Views: 24439

Answers (2)

user12479221
user12479221

Reputation:

Thank you all!!

the trick was this in the package.json:

"start": "tsc-watch --onsuccess \"node dist/index.js\""

Upvotes: 0

Honsemiro
Honsemiro

Reputation: 119

Try typing in the terminal as follows:

ts-node ./src/index.ts

Or, try the following modifications to the "start" script:

"start": "ts-node ./src/index.ts"

Upvotes: 11

Related Questions