本文介绍了为什么DEBUG = foo节点index.js在package.json的scripts部分失败了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 10上,但我正在使用git bash.从git bash运行时,以下内容:

I'm on Windows 10, but I'm using git bash. When running from git bash the following:

DEBUG=foo node index.js

它可以正常工作并设置环境变量DEBUG.但是,如果我将其放入package.json的scripts部分:

it works fine and sets the environment variable DEBUG. But if I put it into the scripts section of the package.json:

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

并从git bash运行以下命令,我得到以下错误:

and run the following from git bash I get the following error:

$ 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.

为什么会这样?

推荐答案

似乎您正在Windows上运行它.

It seems like you're running it on Windows.

假设您按照您所说的操作Bash,我将编写一个脚本:

Assuming that you have Bash working as you said, I would make a script:

#!/bin/bash
DEBUG=foo node index.js

例如run-debug并使用:

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

中的

,以确保Bash解释了DEBUG=foo node index.js命令,而不是command.com或Windows中的任何shell被解释.参见问题#6543:npm脚本shell选择.

in package.json to make sure that the DEBUG=foo node index.js command is interpreted by Bash and not command.com or whatever the shell in Windows is called. See Issue #6543: npm scripts shell select.

对于某些情况,即使您希望它甚至在没有Bash的系统上运行,也要获得最大的跨平台兼容性,最好使用Node脚本而不是Shell脚本:

For cases when you want it to run even on systems without Bash, for maximum cross-platform compatibility, it's even better to use a Node script instead of a shell script:

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

在这种情况下,run-debug.js可能包含以下内容:

In this case the run-debug.js could contain something like:

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

另请参阅:

这篇关于为什么DEBUG = foo节点index.js在package.json的scripts部分失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:38