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

Javascript 如何使用package.json运行ts脚本?,javascript,typescript,package.json,npm-scripts,Javascript,Typescript,Package.json,Npm Scripts,我尝试只使用package.json文件运行typescript文件。因此,运行typescript文件的最低设置是 我只有一个文件:index.ts: console.clear(); console.log("nic to see you!!"); 和package.json文件: { "name": "Todo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": {

我尝试只使用package.json文件运行typescript文件。因此,运行typescript文件的最低设置是

我只有一个文件:index.ts:

console.clear();
console.log("nic to see you!!");
和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"
}


但如果我尝试这样做:

npm启动,我将得到以下错误:

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>
 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
所以我的问题是:我必须修改什么才能编译它

多谢各位

好吧,我现在有了这样的:

{
  "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"
  }
}
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"
  }
}
我安装了打字脚本

我做到了:npm开始。我仍然得到这个错误:

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>
 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
好吧,我现在有了这样的:

{
  "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"
  }
}
如果我这样做:npm运行。我看到这个输出:


> 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.

好了,我现在明白了:

> 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.

但是我在哪里可以看到结果呢?在浏览器中转到某个端口会很好,例如:localhost:4200,但如何做到这一点?

尝试按如下方式在终端中键入:

ts-node ./src/index.ts
或者,尝试对开始脚本进行以下修改:

"start": "ts-node ./src/index.ts"
谢谢大家

诀窍在于package.json中:

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

安装Typescript并首先编译?或使用。或者将文件重命名为.js,因为其中没有类型。这是否回答了您的问题?非常感谢。但这不是问题所在。因为我可以运行角度项目。我已经安装了node.js检查类型脚本安装视频ts节点是需要安装的npm包:npm安装-保存开发ts节点