2014-03-05 37 views
2

是否可以強制我的應用程序在某些特定或較高節點版本下運行? 我發現我的應用程序可能有錯誤,如果某些環境具有較舊的節點。在特定節點版本上運行應用程序

我想知道是否可以在package.json中設置它。如果出現任何問題,它可以將相關消息記錄到終端。

回答

3

您可以使用節點版本管理此:https://github.com/creationix/nvm

安裝NVM後,您可以安裝多個不同版本的節點,只要你想,並且可以選擇下運行一個給定應用,其中之一。

如果你想要你的應用程序只有運行給定的版本,你可以檢查版本:process.versions.node並與之比較。舉例來說,把這個在app.js開始(或任何你最初的文件):

// Using package semver for clarity 
var semver = require('semver'); 
if (!semver.satisfies(process.versions.node, '>0.11.0')) { 
    console.log('Incorrect Node version'); 
    process.exit(); 
} 

(以下僅用於NPM包)

後測試不同版本,你可以指定哪些版本使用engines參數將您的軟件包與package.json兼容。下面的權利要求與等於或大於0.6.0所有版本:

"engines": { 
    "node": ">=0.6" 
} 

應當指出的是,這實際上並沒有強制用戶使用>=0.6,但如果有人試圖npm install會給出錯誤你的包裹。如果你想強制一個版本,你可以添加"engineStrict": true

+0

我試過設置|「engines」:{「node」:「> 0.11.0」},「engineStrict」:true | ,而我的節點env實際上是0.10.25。當我運行npm install時,我會預計會出現一些錯誤或警告顯示,但不會... – George

+0

請參閱編輯回答底部。 – SomeKittens

0

由於@SomeKittens答案描述,您應該在您的package.json中聲明這一點。但是,這並不「強制」任何東西。這只是一個兼容性聲明。如果您確實希望檢測到此情況並採取某些操作(例如記錄警告或退出時出現錯誤),則可以使用process.versions.node獲取正在運行的版本,如semver這樣的包來計算您是否兼容,然後採取相應措施。

然而,通常情況下,這不是通常所做的事情。社區的年齡大約在0.6歲以上,如果你的應用程序無法在所有0.10版本上工作,那就太蹩腳了,甚至0.8的支持通常都是微不足道的,除非你的應用程序涉及到某處節點的內部特性。

npm help json

engines 
    You can specify the version of node that your stuff works on: 

     { "engines" : { "node" : ">=0.10.3 <0.12" } } 

    And, like with dependencies, if you don't specify the version (or if you specify "*" as the ver- 
    sion), then any version of node will do. 

    If you specify an "engines" field, then npm will require that "node" be somewhere on that list. 
    If "engines" is omitted, then npm will just assume that it works on node. 

    You can also use the "engines" field to specify which versions of npm are capable of properly 
    installing your program. For example: 

     { "engines" : { "npm" : "~1.0.20" } } 

    Note that, unless the user has set the engine-strict config flag, this field is advisory only. 

engineStrict 
    If you are sure that your module will definitely not run properly on versions of Node/npm other 
    than those specified in the engines hash, then you can set "engineStrict": true in your pack- 
    age.json file. This will override the user's engine-strict config setting. 

    Please do not do this unless you are really very very sure. If your engines hash is something 
    overly restrictive, you can quite easily and inadvertently lock yourself into obscurity and pre- 
    vent your users from updating to new versions of Node. Consider this choice carefully. If peo- 
    ple abuse it, it will be removed in a future version of npm. 
+0

明白了,我會試着寫在應用程序中。 – George

-1

節點版本管理器(NVM)管理多個Node.js的版本

安裝

克隆Git倉庫從GitHub到〜 /.nvm或您希望的任何其他文件夾。

$ git clone git://github.com/creationix/nvm.git ~/.nvm 

要激活它,請使用source或source來源於bash shell。 -

$ . ~/.nvm/nvm.sh 

如果你想知道什麼。不嘗試這些命令之一 -

$ help source 
$ help . 

它基本上讀取並傳遞給它的文件名執行所有的命令(〜在我們的例子/ .nvm/nvm.sh),在當前shell。

使用

使用它是超級騙子簡單,讓我們看看如何 -

# To check what versions can be installed 
$ nvm ls-remote 

# To install: 
# nvm install [version] 
$ nvm install 0.10.0 

# To check what versions are installed 
$ nvm ls 

# To use the installed version 
$ nvm use 0.10.0 
# .. or just run the node repl with that version 
$ nvm run 0.10.0 

# To remove/uninstall 
$ nvm uninstall 0.10.0 

當您安裝節點v0.10.0,得到它安裝在〜/ .nvm/v0.10.0的和駐留在〜/ .nvm/v0.10.0/bin中的新節點二進制文件將被添加到PATH環境變量中。

$ node -v 
v0.10.3 

$ nvm install 0.10.0 
######################################################################## 100.0% 
Now using node v0.10.0 

$ node -v 
v0.10.0 
相關問題