get the path where npm gulp called from

雨燕双飞 提交于 2020-01-17 07:27:28

问题


New with gulp and npm, I am trying to write a script that takes into account the current directory:

gulp.task('render', function(){
    var folder = process.cwd();
    console.info("I am called from:" + folder);
    //renderThisData(folder + "/data.json");
})

I tried this one as well, it gives same result:

gulp.task('render', function(){
    var folder = process.env.INIT_CWD;
    console.info("I am called from:" + folder);
    //renderThisData(folder + "/data.json");
})

where my tree is like:

root
|--- package.json
|--- gulpfile.js
|--- folder1
|    |--- data.json
|--- folder2
|    |--- data.json
|--- folder3
|    |--- data.json

When I run the script from inside one particular folder, the above script gives always the path where the gulpfile is :-(

D:\root\folder1>npm run render
I am called from D:\root

One work around i found is to use params:

 npm run render -- --folder=folder1

and then read/use the param value in my code. I am wondering if there are other ways to do it?


回答1:


npm run will always run from your root directory (if it's not changed in your script).

You can use process.env.INIT_CWD which is the original cwd it was launched from (Added in version 3.8.1) if you can run gulp 'task' instead of using npm or pass the folder name by parameter npm run render -- --folder=${PWD##*/}



来源:https://stackoverflow.com/questions/41366507/get-the-path-where-npm-gulp-called-from

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!