I have gulpfile.js and package.json in the root directory of the project, but the gulp task itself is not:

Проекта Project Root Directory

📄 gulpfile.js

📁 some_folder

📁 gulp_tasks

1. task1.js

2. task2.js

📄 webpack.js

To download tasks, I use this function:

 function LazyRequireTask(taskName, path, options) { options = options || {}; options.taskName = taskName; gulp.task(taskName, function(callback){ let task = require(path).call(this, options); return task(callback); }); } 

Sample loading task:

 LazyRequireTask('pug to html', 'some_folder/gulp_tasks/pug2html.js'); 

Webpack is one of the gulp task. Webpack and other gulp tasks take some_folder/gulp_tasks/ as the root directory of the project, and this raises a number of problems, but I want to save this organization of tasks. How can I explicitly specify the root directory of the project for all gulp tasks? (In the case of the webpack webpack this will be different).

    1 answer 1

    If I understand you correctly, you need to get the path to the directory where the gulp file runs

    1. This can be done via process.cwd () - returns the current working directory
    2. You can pass the console environment variable explicitly and get the necessary data already in process.evn. [Var_name]

    Example: PATH='/path/your/project' gulp [task_name] and in all tasks you will have process.evn.PATH available

    • Thank you for your reply! The first way is quite good: in fact, all that needs to be done is to pass process.cwd() to the task via options . Everything is simple, it turns out! - Bokov Gleb
    • process.cwd () can be used in the task itself will be the same way as in the gulp file - Igor Ognichenko