javascript - Cum să rulezi un script ts cu package.json?
Încerc să rulez fișiere TypeScript doar cu un fișier package.json. Așadar, o configurație minimă pentru rularea fișierelor TypeScript.
Am doar un singur fișier: index.ts:
console.clear();
console.log("nic to see you!!");
și fișierul package.json:
{
"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"
}
Dar dacă încerc să fac :
npm start, voi primi această eroare:
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 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email 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>
Deci întrebarea mea este: ce trebuie să schimb ca să se compileze?
Mulțumesc
ok, acum îl am așa:
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"
}
}
Și am instalat TypeScript
și eu fac: npm start. Încă primesc această eroare:
npm run build -- -w
> [email 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 build: `tsc -p ./src "-w"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email 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 start: `npm run build -- -w`
npm ERR! Exit status 1
ok, acum îl am așa:
{
"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"
}
}
și dacă fac asta: npm run. Văd această ieșire:
> [email 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.
ok, văd acum asta:
> [email 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.
Dar unde pot vedea rezultatele atunci? Ar fi frumos să merg la un port, cum ar fi: localhost:4200 în browser, dar cum să fac asta?
Răspuns #1
Încercați să scrieți în terminal astfel:
ts-node ./src/index.ts
Sau, încearcă următoarele modificări ale scriptului „start”:
"start": "ts-node ./src/index.ts"
