简体   繁体   English

如何使用 package.json 运行 ts 脚本?

[英]How to run ts script with package.json?

I try to run typescript files with just a package.json file.我尝试仅使用 package.json 文件运行打字稿文件。 So a minimum setup for running typescript files.因此,运行打字稿文件的最低设置。

I just have a singel file: index.ts:我只有一个单文件:index.ts:

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

and package.json file:和 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"
}


But if I try to do :但如果我尝试这样做:

npm start, I will get this error: npm start,我会收到这个错误:

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! Todo@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Todo@1.0.0 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:包.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.我这样做:npm start。 I still get this error:我仍然收到此错误:

 npm run build -- -w


> Todo@1.0.0 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! Todo@1.0.0 build: `tsc -p ./src "-w"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Todo@1.0.0 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! Todo@1.0.0 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.如果我这样做: npm run。 I see this output:我看到这个输出:


> Todo@1.0.0 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:好的,我现在看到这个:

> Todo@1.0.0 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?转到某个端口会很好,例如:浏览器中的 localhost:4200,但是怎么做呢?

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"

Thank you all!!谢谢你们!!

the trick was this in the package.json:诀窍是在 package.json 中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM