2014-06-22 24 views
2

如何使用nodejs構建跨平臺項目?在Windows上開發並構建於Linux上的代碼上使用NodeJs的Grunt?

我在Windows上開發,但我的代碼庫建立在基於Linux的CI服務器上。它使用Grunt構建前端,並且需要幾個nodejs模塊。

Nodejs要求將模塊本地安裝到項目中,以便由grunt.task.loadNpmTasks加載。這個問題是我必須從Windows或Linux的某個地方安裝它們,而當我從一個地方安裝它時,它不能在另一個地方工作。

列表模塊如下我的項目需要在本地安裝:

npm install connect-livereload --production 
npm install time-grunt --production 
npm install load-grunt-tasks --production 
npm install jshint-stylish --production 
npm install load-grunt-tasks --production 
npm install grunt-contrib-copy --production 
npm install grunt-contrib-concat --production 
npm install grunt-contrib-uglify --production 
npm install grunt-contrib-compass --production 
npm install grunt-contrib-jshint --production 
npm install grunt-contrib-cssmin --production 
npm install grunt-contrib-connect --production 
npm install grunt-contrib-clean --production 
npm install grunt-contrib-htmlmin --production 
npm install grunt-contrib-imagemin --production 
npm install grunt-contrib-watch --production 
npm install grunt-rev --production 
npm install grunt-usemin --production 
npm install grunt-mocha --production 
npm install grunt-exec --production 
npm install grunt-open --production 
npm install grunt-svgmin --production 
npm install grunt-concurrent --production 
npm install grunt-ember-templates --production 
npm install grunt-replace --production 
npm install grunt-neuter --production 

如果我從Windows安裝它,然後運行從Windows的項目文件夾grunt還是這一切完美的作品。如果我然後檢查代碼到git中並在linux中創建,chmod 777和chown給我的用戶,並運行相同的grunt命令。它失敗了很多錯誤,像這樣的:

Running "mocha:all" (mocha) task 
Testing: http://localhost:9000/index.html 
Fatal error: spawn ENOENT 

我跑npm install並開始與另一條消息失敗:

Running "mocha:all" (mocha) task 
Testing: http://localhost:9000/index.html 

/home/administrator/platform/frontend/node_modules/grunt-contrib-compass/node_modules/tmp/lib/tmp.js:261 
    throw err; 
     ^
Error: spawn ENOENT 
    at errnoException (child_process.js:998:11) 
    at Process.ChildProcess._handle.onexit (child_process.js:789:34) 

沒有我這樣做也可以正常工作。

因此,從Linux中刪除了我的項目中的整個node_modules目錄並重新運行上面的安裝命令。現在在Linux上完美運行。

然後我檢查它到git中並在Windows中結帳。然後,我去項目文件夾,然後運行grunt,然後它失敗:

Running "mocha:all" (mocha) task 
Testing: http://localhost:9000/index.html 

Running PhantomJS...ERROR 
>> 'c:\Users\Edy' is not recognized as an internal or external command, 
0 [ '\'c:\\Users\\Edy\' is not recognized as an internal or external command,\r', 
>> 'operable program or batch file.' ] 
>> operable program or batch file. 1 [ '\'c:\\Users\\Edy\' is not recognized as an internal or external command,\r', 
>> 'operable program or batch file.' ] 
Warning: PhantomJS exited unexpectedly with exit code 1. Use --force to continue. 

卸載phantomjs並安裝在本地沒有幫助。全球安裝也無濟於事。因此,使它在Windows上工作的唯一方法似乎是刪除node_modules目錄並在Windows上重新安裝,這使我首次遇到Linux問題。

有沒有辦法在像我這樣的跨平臺環境中使用nodejs的東西?我不能相信我是第一個有這樣的設置在那裏嘿嘿

任何提示或幫助在此表示讚賞。謝謝!

回答

3

node.js中的許多依賴項使用本機插件。當你使用npm install時,你的特定環境會編譯原生插件。

如果您在環境之間移動,則可以使用npm rebuild重新構建新環境的二進制文件。或者更冗長的方法,再次刪除node_modules文件夾和npm install

Fatal error: spawn ENOENT表示一個進程node.js正在嘗試產生不存在。當試圖產生爲另一個環境編譯的二進制文件時,它所期望的二進制文件不存在。

+1

哦,非常感謝,'npm rebuild'就是我所缺失的......像一個魅力一樣工作! –