Node.js NODE_PATH environment variable

后端 未结 3 1589
后悔当初
后悔当初 2021-01-12 08:55

During development I used to WebStorm node_path =. environment variable. I have set up a variable in the launch of the project settings. Now I

相关标签:
3条回答
  • 2021-01-12 09:14

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

    0 讨论(0)
  • 2021-01-12 09:22

    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

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

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

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

    0 讨论(0)
  • 2021-01-12 09:22

    Add

    export NODE_PATH=...

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

    or

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

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

    and run

    npm install

    in the same folder instead. Hope it helps.

    0 讨论(0)
提交回复
热议问题