简体   繁体   English

在 Kubuntu 18.04 上运行在 Nodejs 中设置环境变量的 NPM 模块时权限被拒绝

[英]Permission denied when running NPM module that sets environment variables in Nodejs on Kubuntu 18.04

I am trying to set environment variables on process.env in nodejs using the npm module env-cmd.我正在尝试使用 npm 模块 env-cmd 在 nodejs 中的 process.env 上设置环境变量。 My system is running Kubuntu 18.04.我的系统运行的是 Kubuntu 18.04。

I consistently get "permission denied" even when using sudo.即使在使用 sudo 时,我也始终得到“权限被拒绝”。

My node and npm versions are the ones from the Ubuntu package manager.我的 node 和 npm 版本是来自 Ubuntu 包管理器的版本。 So not the latest, but the latest "approved" ones.所以不是最新的,而是最新的“批准”的。

I would like to get past this permission denied thing.我想通过这个权限被拒绝的事情。

I have tried updating env-cmd to the latest package and using the -f switch with the exact same results.我尝试将 env-cmd 更新到最新的软件包并使用 -f 开关获得完全相同的结果。 I have verified my file path is correct and double checked my code.我已经验证我的文件路径是正确的,并仔细检查了我的代码。 Which leads me to think this is a "linux thing"这让我认为这是一个“Linux 的东西”

from package.json来自package.json

"scripts": {
    "start": "node src/index.js",
    "dev": "env-cmd ./config/dev.env nodemon src/index.js"
  },
"devDependencies": {
    "env-cmd": "^8.0.2",
    "nodemon": "^1.18.9"
  }

from dev.env来自dev.env

PORT=3000

from index.js来自index.js

const port = process.env.PORT;

The expected result is that node runs and variables get set on process.env预期结果是节点运行并在 process.env 上设置变量

Terminal output is终端输出是

hoo@BadWolf:~/storage/node-task-api v3$ npm run dev

> node-task-manager@1.0.0 dev /home/hoo/storage/node-task-api v3
> sudo env-cmd ./config/dev.env nodemon src/index.js

[sudo] password for hoo: 
sudo: env-cmd: command not found

npm ERR! Linux 4.15.0-50-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "run" "dev"
npm ERR! node v8.10.0
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! node-task-manager@1.0.0 dev: `sudo env-cmd ./config/dev.env nodemon src/index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node-task-manager@1.0.0 dev script 'sudo env-cmd ./config/dev.env nodemon src/index.js'.
....(details on how to report an issue)
npm ERR!     /home/hoo/storage/node-task-api v3/npm-debug.log

debug.log:调试日志:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'run', 'dev' ]
2 info using npm@3.5.2
3 info using node@v8.10.0
4 verbose run-script [ 'predev', 'dev', 'postdev' ]
5 info lifecycle node-task-manager@1.0.0~predev: node-task-manager@1.0.0
6 silly lifecycle node-task-manager@1.0.0~predev: no script for predev, continuing
7 info lifecycle node-task-manager@1.0.0~dev: node-task-manager@1.0.0
8 verbose lifecycle node-task-manager@1.0.0~dev: unsafe-perm in lifecycle true
9 verbose lifecycle node-task-manager@1.0.0~dev: PATH: /usr/share/npm/bin/node-gyp-bin:/home/hoo/storage/node-task-api v3/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
10 verbose lifecycle node-task-manager@1.0.0~dev: CWD: /home/hoo/storage/node-task-api v3
11 silly lifecycle node-task-manager@1.0.0~dev: Args: [ '-c', 'env-cmd ./config/dev.env nodemon src/index.js' ]
12 silly lifecycle node-task-manager@1.0.0~dev: Returned: code: 126  signal: null
13 info lifecycle node-task-manager@1.0.0~dev: Failed to exec dev script
14 verbose stack Error: node-task-manager@1.0.0 dev: `env-cmd ./config/dev.env nodemon src/index.js`
14 verbose stack Exit status 126
14 verbose stack     at EventEmitter.<anonymous> (/usr/share/npm/lib/utils/lifecycle.js:232:16)
14 verbose stack     at emitTwo (events.js:126:13)
14 verbose stack     at EventEmitter.emit (events.js:214:7)
14 verbose stack     at ChildProcess.<anonymous> (/usr/share/npm/lib/utils/spawn.js:24:14)
14 verbose stack     at emitTwo (events.js:126:13)
14 verbose stack     at ChildProcess.emit (events.js:214:7)
14 verbose stack     at maybeClose (internal/child_process.js:925:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
15 verbose pkgid node-task-manager@1.0.0
16 verbose cwd /home/hoo/storage/node-task-api v3
17 error Linux 4.15.0-50-generic
18 error argv "/usr/bin/node" "/usr/bin/npm" "run" "dev"
19 error node v8.10.0
20 error npm  v3.5.2
21 error code ELIFECYCLE
22 error node-task-manager@1.0.0 dev: `env-cmd ./config/dev.env nodemon src/index.js`
22 error Exit status 126
23 error Failed at the node-task-manager@1.0.0 dev script 'env-cmd ./config/dev.env nodemon src/index.js'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the node-task-manager package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     env-cmd ./config/dev.env nodemon src/index.js
....(details on how to report an issue)
24 verbose exit [ 1, true ]

Solved this with some help from Adam Hartleb, a Course Assistant at Udemy.在 Udemy 的课程助理 Adam Hartleb 的帮助下解决了这个问题。 He suggested running it with npx, which worked, after some finagling.他建议使用 npx 运行它,经过一番折腾,它奏效了。

For anyone else having this problem, here is what I did:对于遇到此问题的其他人,这是我所做的:

I solved this by inspecting the source code for env-cmd, then re-reading the docs.我通过检查 env-cmd 的源代码,然后重新阅读文档解决了这个问题。

The problem is that, in Ubuntu 18.04 at least, env-cmd doesn't play well with non-default file locations.问题是,至少在 Ubuntu 18.04 中,env-cmd 不能很好地处理非默认文件位置。

To get it to run, you must have a version of Node that supports npx.要让它运行,你必须有一个支持 npx 的 Node 版本。 If you are in Ubuntu then you have to use a version of node that is not supported by Canonical.如果您使用的是 Ubuntu,那么您必须使用 Canonical 不支持的节点版本。 The way I solved that was to install NVM and Node 10.15.3我解决的方法是安装 NVM 和 Node 10.15.3

how to install NVM and Node LTS 如何安装 NVM 和 Node LTS

You will need to use the default file location for your .env file:您将需要使用 .env 文件的默认文件位置:

 ./.env

You will need to remove any references to non-default file locations,as their presence will only anger env-cmd.您需要删除对非默认文件位置的任何引用,因为它们的存在只会激怒 env-cmd。 It will have a literal hangup.它将有一个字面上的挂断。 (it will actually spawn an error called "hangup"). (它实际上会产生一个称为“挂断”的错误)。 It will also use the word "spawn".它也会使用“spawn”这个词。 I only tell you what I saw.我只告诉你我看到的。

I changed my "dev" script to我将“开发”脚本更改为

"dev" : "npx env-cmd nodemon src/index.js"

It should work now.它现在应该可以工作了。 May the .env-gods be merciful upon your app.愿 .env-gods 对您的应用程序仁慈。

BACKSTORY: How I got there背景故事:我是如何到达那里的

Adam's suggestion of "npx env-cmd ./config/dev.env nodemon src/index.js" resulted in:亚当对“npx env-cmd ./config/dev.env nodemon src/index.js”的建议导致:

hoo@BadWolf:~/storage/node-task-api v3$ npx env-cmd ./config/dev.env nodemon src/index.js
(node:19209) UnhandledPromiseRejectionWarning: Error: Unable to locate env file at default location (./.env)
    at /home/hoo/storage/node-task-api v3/node_modules/env-cmd/dist/get-env-vars.js:47:19
    at Generator.throw (<anonymous>)
    at rejected (/home/hoo/storage/node-task-api v3/node_modules/env-cmd/dist/get-env-vars.js:5:65)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
    at findNodeScript.then.existing (/home/hoo/.nvm/versions/node/v10.15.3/lib/node_modules/npm/node_modules/libnpx/index.js:268:14)
(node:19209) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:19209) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I traced the problem back to a few undefined variables in the module code, which I logged to terminal.我将问题追溯到模块代码中的一些未定义变量,我将这些变量登录到终端。

hoo@BadWolf:~/storage/node-task-api v3$ npx env-cmd ./config/dev.env nodemon src/index.js
Russ logging from get-env-vars.js 27:  getEnvFile({ filePath, fallback }): filePath = undefined
Russ logging from get-env-vars.js 27: getEnvFile({ filePath, fallback }): fallback = undefined

Creating a .env file at ./.env and running the same npx script (with the non-default file argument)在 ./.env 处创建一个 .env 文件并运行相同的 npx 脚本(使用非默认文件参数)

hoo@BadWolf:~/storage/node-task-api v3$ npx env-cmd ./config/dev.env nodemon src/index.js
spawn ./config/dev.env EACCES
Hangup

With the non-default file location removed, to spin up the server with env-cmd run:删除非默认文件位置后,使用 env-cmd 运行服务器:

npx env-cmd  nodemon src/index.js

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

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