如何使用 package.json 运行 ts 脚本?
Posted
技术标签:
【中文标题】如何使用 package.json 运行 ts 脚本?【英文标题】:How to run ts script with package.json? 【发布时间】:2020-04-15 11:53:46 【问题描述】:我尝试仅使用 package.json 文件运行 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 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>
所以我的问题是:我必须改变什么才能编译?
谢谢
好吧,我现在是这样的:
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 start。我仍然收到此错误:
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 run。我看到了这个输出:
> 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,但是怎么做呢?
【问题讨论】:
安装Typescript并先编译? 或使用npmjs.com/package/ts-node。或者只是将文件重命名为 .js,因为其中没有类型。 这能回答你的问题吗? How to run typescript compiler as a package.json script without grunt or gulp 谢谢。但这不是问题。因为我可以运行 Angular 项目。我已经安装了 node.js 查看打字稿设置视频youtube.com/… 【参考方案1】:谢谢大家!!
诀窍是在 package.json 中:
"start": "tsc-watch --onsuccess \"node dist/index.js\""
【讨论】:
【参考方案2】:尝试在终端中输入如下:
ts-node ./src/index.ts
或者,尝试对“启动”脚本进行以下修改:
"start": "ts-node ./src/index.ts"
【讨论】:
ts-node
是需要安装的npm包:npm install --save-dev ts-node
以上是关于如何使用 package.json 运行 ts 脚本?的主要内容,如果未能解决你的问题,请参考以下文章
记一次使用pm2运行node+ts项目,ts-node报错问题To load an ES module, set “type“: “module“ in the package.json or use
如何在没有 grunt 或 gulp 的情况下将 typescript 编译器作为 package.json 脚本运行
你可以在 package.json 中为 npm 脚本设置别名吗?
如何在无需全局安装 ts-node 或 npx 的情况下完全运行 nodemon + ts-node + typescript?