【编程开发工具】vscode npm怎样实行
2019-11-19开发工具搜奇网57°c
A+ A-vscode npm怎样实行?
vscode 调试node之npm与nodemon
调试nodejs有许多体式格局,能够看这一篇How to Debug Node.js with the Best Tools Available,个中我最喜欢运用的照样V8 Inspector和vscode的体式格局。
在vscode中,点击谁人蜘蛛的按钮
就可以看涌现debug的侧栏,接下来增加设置
挑选环境
就可以看到launch.json的文件了。
启动的时候,挑选响应的设置,然后点击指向右边的绿色三角
launch形式与attach形式
{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceRoot}/index.js" }, { "type": "node", "request": "attach", "name": "Attach to Port", "address": "localhost", "port": 5858 } ] }
当request为launch时,就是launch形式了,这是顺序是从vscode这里启动的,假如是在调试那将一向处于调试的形式。而attach形式,是衔接已启动的效劳。比方你已在表面将项目启动,倏忽须要调试,不须要关掉已启动的项目再去vscode中重新启动,只要以attach的形式启动,vscode能够衔接到已启动的效劳。当调试完毕了,断开衔接就好,显著比launch更轻易一点。
在debug中运用npm启动
许多时候我们将很长的启动敕令及设置写在了package.json的scripts中,比方
"scripts": { "start": "NODE_ENV=production PORT=8080 babel-node ./bin/www", "dev": "nodemon --inspect --exec babel-node --presets env ./bin/www" },
我们愿望让vscode运用npm的体式格局启动并调试,这就须要以下的设置
{ "name": "Launch via NPM", "type": "node", "request": "launch", "runtimeExecutable": "npm", "runtimeArgs": [ "run-script", "dev"//这里的dev就对应package.json中的scripts中的dev ], "port": 9229//这个端口是调试的端口,不是项目启动的端口 },
在debug中运用nodemon启动
仅仅运用npm启动,虽然在dev敕令中运用了nodemon,顺序也能够一般的重启,可重启了以后,调试就断开了。所以须要让vscode去运用nodemon启动项目。
{ "type": "node", "request": "launch", "name": "nodemon", "runtimeExecutable": "nodemon", "args": ["${workspaceRoot}/bin/www"], "restart": true, "protocol": "inspector",//相当于--inspect了 "sourceMaps": true, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "runtimeArgs": [//对应nodemon --inspect以后除了启动文件以外的其他设置 "--exec", "babel-node", "--presets", "env" ] },
注重这里的runtimeArgs,假如这些设置是写在package.json中的话,就是如许的
nodemon --inspect --exec babel-node --presets env ./bin/www
如许就很轻易了,项目能够一般的重启,每次重启一样会开启调试功用。
但是,我们并不想时候开启调试功用怎样办?
这就须要运用上面说的attach形式了。
运用以下的敕令一般的启动项目
nodemon --inspect --exec babel-node --presets env ./bin/www
当我们想要调试的时候,在vscode的debug中运转以下的设置
{ "type": "node", "request": "attach", "name": "Attach to node", "restart": true, "port": 9229 }
圆满!
以上就是vscode npm怎样实行的细致内容,更多请关注ki4网别的相干文章!