简体   繁体   English

Node.js NODE_PATH环境变量

[英]Node.js NODE_PATH environment variable

During development I used to WebStorm node_path =. 在开发过程中我习惯了WebStorm node_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! 帮助解决问题!

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

Add

export NODE_PATH=... export 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 npm安装

in the same folder instead. 而是在同一个文件夹中。 Hope it helps. 希望能帮助到你。

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.json like 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM