简体   繁体   中英

How to pass environment variables to pkg(node js)?

I'm working on packaging my Node.js project into an executable. For that I'm using "pkg" tool ( https://www.npmjs.com/package/pkg#detecting-assets-in-source-code )

pkg -t node6-linux-x64 . -d

"package.json"(using better-npm-run):

"env": {
  "NODE_ENV": "development",
  "NODE_PATH": "../shared/config:../shared/lib:./config:./lib:./relay",
  ..
}

I have also tried setting OS environment variables, but pkg didn't accept it too.

export NODE_PATH=/path/from/root/shared/config:/path/from/root/shared/lib:/path/from/root/config:/path/from/root/lib:/path/from/root/relay
OR
export PATH=${PATH}:/path/from/root/shared/config:/path/from/root/shared/lib:/path/from/root/config:/path/from/root/lib:/path/from/root/relay

I have also tried below solution, it does help in solving few warning,

"pkg":{
  "scripts": ["config/**/*.js", "lib/**/*.js", "relay/server/**/*.js", "../shared/config/**/*.js", "../shared/lib/**/*.js"]
}

but my application uses local requires(), so it creates more such warnings. As my application is complex it is difficult to change all the local requires.

Also is it correct way to access parent directory inside "scripts" (package.json)?

"pkg":{
  "scripts": ["../shared/config/**/*.js", "../shared/lib/**/*.js"]
}

Thanks in advance

So here you just want these two env variables to be set before your script runs an easy way to do this is like this:

NODE_ENV=development NODE_PATH=../shared/config:../shared/lib:./config:./lib:./relay pkg -t node6-linux-x64 . -d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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