javascript Node.js NODE_PATH 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20185627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:58:02  来源:igfitidea点击:

Node.js NODE_PATH environment variable

javascriptnode.jswebstorm

提问by mazorati

During development I used to WebStormnode_path =.environment variable. I have set up a variable in the launch of the project settings. Now I want to try to run the project on the server, but do not know how to set this variable there. Help solve the problem!

在开发过程中,我习惯于使用WebStormnode_path =.环境变量。我已经在启动项目设置中设置了一个变量。现在我想尝试在服务器上运行该项目,但不知道如何在那里设置此变量。帮助解决问题!

采纳答案by Tom Grant

Assuming it's a UNIX or Mac OS X server, use export NODE_PATH=and append the path you want.

假设它是 UNIX 或 Mac OS X 服务器,请使用export NODE_PATH=并附加所需的路径。

回答by mr haven

I would recommend setting the variable right before you run the command like so:

我建议在运行命令之前设置变量,如下所示:

NODE_PATH=src/ node myapp.js

This way the variable is set when needed. This is preferable unless you really need to change the path with different versions of your deployment.

这样,在需要时设置变量。除非您确实需要使用不同版本的部署更改路径,否则这是更可取的。

If on windows, you can use this lil package to get the effect to work so it is consistent across dev and prod: win-node-env

如果在 Windows 上,您可以使用这个 lil 包来获得效果,使其在 dev 和 prod 之间保持一致:win-node-env

For bonus points add it to your start script in package.jsonlike so:

对于奖励积分,将其添加到您的启动脚本中,package.json如下所示:

"scripts": {
    "start": "NODE_PATH=src/ node myapp.js"
}

Then in production all you need to do is run: npm start

然后在生产中你需要做的就是运行: npm start

回答by VernZheng

Add

添加

export NODE_PATH=...

导出 NODE_PATH=...

to your system environment setting (/etc/profile,~/.bash_profile...), make it works.

到您的系统环境设置(/etc/profile,~/.bash_profile...),使其工作。

or

或者

You can declare dependencies in package.json(project), like this:

您可以在 package.json(project) 中声明依赖项,如下所示:

{
    ...
    "dependencies": {
        "connect": "~2.0.3",
        ...
    },
    ...
}

and run

并运行

npm install

安装

in the same folder instead. Hope it helps.

在同一个文件夹中。希望能帮助到你。