Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start node server and export NODE_PATH (with npm start or inline command)

Before starting the server, I need to export ('set' actualy, I'm using Win7) NODE_PATH variable. I tried to do it with this command (not working even in command line):

set NODE_PATH=./ && node server.js

and for package.json:

  "scripts": {
    "start": "set NODE_PATH=. && node server.js"
  },

But it's not working. I got Error: Cannot find module which appears only if NODE_PATH not specified.

So, how this problem could be solved? I need a proper way to export NODE_PATH and run the server with inline command, or a way to specify two separate commands for 'start' script.

like image 450
Maksim Nesterenko Avatar asked May 17 '15 00:05

Maksim Nesterenko


People also ask

What is npm start in node JS?

npm start: npm start script is used to execute the defined file in it without typing its execution command. Package.json file "scripts"{ "start":"node index.js" } index.js.


2 Answers

set NODE_PATH=./ ; && node server.js

On Windows, the command above works fine. And in general, it should work without a semicolon when you set environment variables. But here, probably, path string causes the problem?

Also notice that you should set environment variables in a different way for Linux, no set keyword must be used. If your scripts will be run on a different platform, probably it's better to use some plugin like cross-env to write your scripts.

This question helps Setting an environment variable before a command in Bash is not working for the second command in a pipe

like image 90
Maksim Nesterenko Avatar answered Oct 28 '22 17:10

Maksim Nesterenko


Below is what worked for me in Linux (CentOS). I needed this because the node modules in use were all installed globally.

"scripts": {
    "start": "export NODE_PATH=/usr/lib/node_modules && node myscript.js"
},

Use export instead of set. No semicolon needed.

like image 1
Aaron Cicali Avatar answered Oct 28 '22 19:10

Aaron Cicali