简体   繁体   English

如果不可用则如何安装节点依赖Gulp + nodejs

[英]How to install node dependencies if they are not available Gulp + nodejs

I am using Gulp to start a web application. 我正在使用Gulp启动Web应用程序。 I have the following basic code for my gulpfile.js: 我的gulpfile.js具有以下基本代码:

var gulp = require('gulp'),
nodemon = require('gulp-nodemon');

gulp.task('default', function () {
  nodemon({
    script: 'server.js'
  , ext: 'js html'
  , env: { 'NODE_ENV': 'development' }
  })
})

Using Gulp, I want to check for dependencies and if they are not available then install them and then run 'script.js'. 使用Gulp,我要检查依赖项,如果不可用,请安装它们,然后运行'script.js'。 How can this be done? 如何才能做到这一点?

I have the following package.json: 我有以下package.json:

{
"name": "sample-project",
"version": "1.0.0",
"description": "Displays users and user details",
"main": "server.js",
"dependencies": {
"jquery"  : “>=1.5.1",
“bootstrap”: ">= 3.0.0”
}
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "Arihant Jain",
"license": "ISC"
}

You can run npm install independently from an executing task using node's child_process as such: 您可以使用节点的child_process这样独立于正在执行的任务来独立运行npm install

var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
var child_process = require('child_process');

gulp.task('default', function () {

    // Run npm install from the child process
    child_process.exe('npm install', function(err, stdout, stderr){

        // if everything goes well
        if(!err){

             // run nodemon
              nodemon({
                script: 'server.js'
              , ext: 'js html'
              , env: { 'NODE_ENV': 'development' }
              })

        }

    });
})

Given your requirement: 根据您的要求:

Using Gulp, I want to check for dependencies and if they are not available then install them... 使用Gulp,我要检查依赖项,如果不可用,请安装它们。

That is exactly what npm install does. 这正是npm install所做的。 It checks the local package.json and proceeds to install missing packages. 它检查本地package.json并继续安装缺少的软件包。

So, I worked it around in a way by using gulp-run. 因此,我通过使用gulp-run解决了这一问题。 I actually run the command npm install . 我实际上运行命令npm install

gulpfile looks like this: gulpfile看起来像这样:

        var gulp = require('gulp'),
        nodemon = require('gulp-nodemon')
        run = require('gulp-run')
        runSequence = require('run-sequence')
        open = require('gulp-open');

gulp.task('default', function() {
  runSequence('dependencies',
              'start',
              'uri');
});


      gulp.task('dependencies', function() {
  return run('npm install').exec();
})

    gulp.task('uri', function(){
  gulp.src(__filename)
  .pipe(open({uri: 'http://localhost:3000/index.html'}));
});



    gulp.task('start', function () {
  nodemon({
    script: 'server.js'
  , ext: 'js html'
  , env: { 'NODE_ENV': 'development' }
  })
}) 

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

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