2016-12-29 10 views
2

私はWindows 10ですが、git bashを使用しています。 git bashから以下を実行している場合:`package.json`の` scripts`セクションで `DEBUG = foo node index.js`が失敗するのはなぜですか?

DEBUG=foo node index.js 

それが正常に動作し、環境変数DEBUGを設定します。私はpackage.jsonscriptsセクションにそれを置く場合でも:

"scripts": { 
    "start": "DEBUG=foo node index.js" 
    }, 

git bashから以下を実行して、私は次のエラーを取得する:

$ npm start 

> [email protected] start C:\Users\maksym.koretskyi\Desktop\nodejs 
> DEBUG=foo node index.js 

'DEBUG' is not recognized as an internal or external command, 
operable program or batch file. 

なぜ行動?

+2

あなたのエラーは、 'bash'シェルではなく' Windows'環境( 'batch'キーワードに注意してください)で実行された命令の結果として生じます。 – Inian

+0

ありがとう、それは私が思ったものです。あなたが 'git bash'から' npm start'を実行したとしても、なぜWindows環境下で 'DEBUG = foo node index.js'が実行されるのか知っていますか? –

+0

'node'がperoperlyにインストールされている場合、' Git bash'の '$ PATH'をチェックしてノードがインストールされているパスがあるかどうか確認することができます – Inian

答えて

2

Windows上で実行しているようです。

例えば呼ば
#!/bin/bash 
DEBUG=foo node index.js 

:あなたはbashのは、あなたが言ったように、私はスクリプトになるだろう働いていると仮定すると

run-debugとは使用:package.json

"scripts": { 
    "start": "bash run-debug" 
}, 

DEBUG=foo node index.jsコマンドはバッシュによって解釈され、command.comまたはWindowsシェルが呼ばれないどんなことを確認します。 Issue #6543: npm scripts shell selectを参照してください。

例については

あなたはそれもバッシュのないシステム上で実行する、最大のクロスプラットフォームの互換性のために、それは代わりにシェルスクリプトのノードのスクリプトを使用するようにしても良いでしょう。この場合

"scripts": { 
    "start": "node run-debug.js" 
}, 

run-debug.jsは次のようなものを含めることができます:

let env = Object.create(process.env); 
env.DEBUG = 'foo'; 
spawn('node', ['app.js'], {env}); 

も参照してください:

+0

ありがとう、それは解決策かもしれませんが、' npm'がWindows上でコマンドを実行する理由を理解したいのですが、 'git bash'ではなく –

1

はい、確かに、私は次のようthreadを見つけたのだが、と述べている:

The shell config variable is only used by the npm explore command, which forks a subshell. It's not meant to be used to allow you to use, say, bash on Windows, or switch between Powershell and cmd.exe. Scripts are always run by the shebang line on Unix, and cmd.exe on Windows.

だから、それはデザインによってだと思われるし、それを変更する方法はありません。

関連する問題