Windows环境使用命令npx nodemon --exec babel-node src/index.js监听js文件变化报错,监听不生效

Windows环境使用命令npx nodemon --exec babel-node src/index.js监听js文件变化报错,监听不生效

Windows环境使用命令npx nodemon --exec babel-node src/index.js监听js文件变化报错,监听不生效


F:\hyworkspace\study\koa_p1>npx nodemon --exec babel-node src/index.js
[nodemon] 2.0.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `babel-node src/index.js`
[nodemon] restarting due to changes...
(node:7432) UnhandledPromiseRejectionWarning: Error: Command failed: wmic process where (ParentProcessId=2452) get ProcessId 2> nul
    at checkExecSyncError (child_process.js:611:11)
    at execSync (child_process.js:647:15)
    at kill (F:\hyworkspace\study\koa_p1\node_modules\nodemon\lib\monitor\run.js:337:26)
    at Function.run.kill (F:\hyworkspace\study\koa_p1\node_modules\nodemon\lib\monitor\run.js:425:7)
    at Bus.<anonymous> (F:\hyworkspace\study\koa_p1\node_modules\nodemon\lib\monitor\run.js:495:7)
    at Bus.emit (events.js:327:22)
    at restartBus (F:\hyworkspace\study\koa_p1\node_modules\nodemon\lib\monitor\watch.js:228:7)
    at FSWatcher.filterAndRestart (F:\hyworkspace\study\koa_p1\node_modules\nodemon\lib\monitor\watch.js:212:16)
    at FSWatcher.emit (events.js:315:20)
    at FSWatcher.emitWithAll (F:\hyworkspace\study\koa_p1\node_modules\chokidar\index.js:540:8)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7432) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function w
ithout a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection
, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7432) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled wil
l terminate the Node.js process with a non-zero exit code.

之前未使用babel-node时,代码未改成es6语法时,运行npx src/index.js可正常监听js文件变化

package.json如下:
{
  "name": "koa_p1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec babel-node src/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@koa/cors": "^3.1.0",
    "koa": "^2.13.1",
    "koa-body": "^4.2.0",
    "koa-combine-routers": "^4.0.2",
    "koa-helmet": "^6.1.0",
    "koa-json": "^2.0.2",
    "koa-router": "^10.0.0",
    "koa-static": "^5.0.0"
  },
  "devDependencies": {
    "@babel/core": "^7.14.6",
    "@babel/node": "^7.14.7",
    "@babel/preset-env": "^7.14.7",
    "babel-loader": "^8.2.2",
    "clean-webpack-plugin": "^4.0.0-alpha.0",
    "cross-env": "^7.0.3",
    "nodemon": "^2.0.9",
    "webpack": "^5.41.1",
    "webpack-cli": "^4.7.2",
    "webpack-node-externals": "^3.0.0"
  }
}

webpack.config.json如下:
const path = require('path');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const WebpackNodeExternals = require('webpack-node-externals');

const config = {
    target:'node',
    mode:'development',
    entry:[path.join(__dirname,'src/index.js')],
    output:{
        filename:'[name].bundle.js',
        path:path.join(__dirname,'./dist')
    },
    module:{
        rules:[
        {
            test:/\.(js|jsx)$/,
            loader:'babel-loader',
            exclude:path.join(__dirname,'./node_modules')
        }
    ]
    },
    externals:[WebpackNodeExternals()],
    plugins:[new CleanWebpackPlugin()],
    node:{
        //console:true,
        global:true,
        //process:true,
        //Buffer:true,
        __dirname:true,
        __filename:true,
        //setImmediate:true,
        //path:true
    }
}

module.exports = config

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
Brian 2021-07-09 12:35:27

如果用babel-node那就跟webpack没关系,babel能直接解析ES6语法

  • 提问者 精慕门2108970 #1

    可是nodemon监听就不生效了,文件变化之后直接就报错如上了,感觉是nodemon内部监听上出了点问题,服务是可以正常启动运行的

    2021-07-09 12:41:39
  • Brian 回复 提问者 精慕门2108970 #2

    你可以把你的代码发一下?

    2021-07-11 22:29:05
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师