2016-11-29 83 views
1

我想在gruntfile.js中設置node_modules目錄的路徑。我想爲我所有的grunt項目設置一箇中心node_module目錄。爲gruntfile.js中的node_modules設置目錄

是否可以設置外部node_module目錄的路徑?

當前設置(工作)

 
projekt1 
--gruntfile.js 
--package.json 
--node_modules 

projekt2 
--gruntfile.js 
--package.json 
--node_modules 

建立,我想有

 
node_modules 

project1 
--gruntfile.js 
--package.json 

project2 
--gruntfile.js 
--package.json 

+0

嘗試,我認爲它接近你正在努力實現 http://stackoverflow.com/questions/29786887/how-can什麼-i-MAKE-多項目共享節點模塊目錄 – rule

回答

0

您可以....我一直在使用此設置沒有任何的問題。在每個項目的Gruntfile.js文件,只需將底座設置爲一個目錄像這樣:

鑑於這樣的文件夾結構:

node_modules 
    project1 
     app.js 
     Gruntfile.js 
     package.json 

Gruntfile.js可能是這個樣子

module.exports = function(grunt) { 

    // Tell grunt where to find node_modules 
    // This is the line you're looking for 
    grunt.file.setBase('../'); 

    // Your normal grunt config 
    grunt.initConfig({ 

     // Your package.json file is now relative to the parent folder 
     pkg: grunt.file.readJSON('project1/package.json'), 

     // I use webpack, your tasks will probably be different 
     webpack: { 
      options: {}, 
      build: { 

       // Paths are now resolved from the upper folder 
       // since we set the base path to one level up 
       entry: './project1/app.js', 
       output: { 

        // Again, the path must be relative to the parent folder 
        path: 'project1', 
        filename: 'app.min.js' 
       } 
      } 
     } 

     // etc... 
    }); 

    // Load tasks... 

} 

一旦你setBase('../')你需要確保所有的文件路徑得到正確解析,因爲你的基路徑已經改爲1級了。

警告......如果您發佈或分發您的軟件包,Grunt任務將無法爲下載它的人員工作,因爲他們很可能不會與node_modules 1級別的設置具有相同的設置。而且即使他們這樣做,他們可能沒有命名project1文件夾中同樣在這裏