简体   繁体   English

使用nodemon命令运行npm脚本

[英]Run npm script with nodemon command

I am testing GraphQL Server from Apollo and what to integrate nodemon to it. 我正在测试Apollo的GraphQL Server以及将nodemon集成到它的内容。 Here's sample file structure : 这是示例文件结构:

build/
src/
server.js

Here my npm scripts looks like this 这里我的npm脚本看起来像这样

"scripts": {
   "start": "babel --presets es2015,stage-2 server.js -d build/  &&  node build/server.js",
   "dev": "nodemon server.js" // Sample code here
}

What npm run start would do is to convert ES6 code into build/server.js using babel and execute it. npm run start要做的是使用babel将ES6代码转换为build/server.js并执行它。 This would start the server correctly. 这将正确启动服务器。

What I want is to watch for changes in server.js or in src/ and restart the server if changes occur. 我想要的是监视server.jssrc/更改,并在发生更改时重新启动服务器。 Here I want to execute npm run start command if any changes occur. 如果发生任何变化,我想执行npm run start命令。 What is the correct 'nodemon' command to my need. 什么是正确的'nodemon'命令符合我的需要。 Its better if I could use npm run dev like command to start development using nodemon. 如果我可以使用npm run dev like命令来启动使用nodemon的开发,那就更好了。

you can use gulpjs to watch any changes in specific folders and then command it to do something. 您可以使用gulpjs观察特定文件夹中的任何更改,然后命令它执行某些操作。 With your sample, you want to convert the code to es6 too. 对于您的示例,您还希望将代码转换为es6。 so it also need gulp-bable and 所以它也需要gulp-bable . you can include babel-preset-stage-2 if you want. 如果你愿意,你可以包括babel-preset-stage-2 So you can simply put the code below in gulpfile.js 所以你可以简单地将下面的代码放在gulpfile.js中

gulp.task('build-es2015', () => {
    return gulp.src('server.js')
        .pipe(babel({
            presets: ['es2015']
        }))
        .pipe(gulp.dest('build'));
});
gulp.task('watch', () => {
    gulp.watch(['./app/*.js'], ['build-es2015'])
})

Basically, the task 'watch' will keep watching the specific files. 基本上,“监视”任务将继续观察特定文件。 When they are saved then it will to execute the task 'build-es2015' to convert to es6. 当它们被保存时,它将执行任务'build-es2015'以转换为es6。

And then nodemon, it needs gulp-nodemon then you can do on gulpfile.js 然后是nodemon,它需要gulp-nodemon然后你可以在gulpfile.js上做

gulp.task('server', () => {
  nodemon({
    script: 'build/server.js',
    ext: 'js',
    ignore: [
      'server.js',
      'node_modules/**',
      'test/**',
      'build/**'
    ]
  })
  .on('restart', () => { console.log(`Server restarted!`) })
})

The above will keep watching build/server.js'. 以上将继续关注build / server.js'。 whenever it is changed, nodemon will automatically restart the server. 无论何时更改,nodemon都会自动重启服务器。

And the last piece for gulpfile.js 而gulpfile.js的最后一块

gulp.task('dev', ['server', 'watch'])

Include the tasks that need to be executed for gulp command. 包括gulp命令需要执行的任务。

$ gulp dev

or with npm command 或者使用npm命令

"scripts": {
  "start": "gulp dev"
}

so you can npm run start as well. 所以你也可以npm run start

And dont forget to require all packages in gulpfile.js 并且不要忘记要求gulpfile.js中的所有包

const gulp       = require('gulp')
const babel      = require('gulp-babel')
const nodemon    = require('gulp-nodemon')

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

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