【问题标题】:Permission denied when running NPM module that sets environment variables in Nodejs on Kubuntu 18.04在 Kubuntu 18.04 上运行在 Nodejs 中设置环境变量的 NPM 模块时权限被拒绝
【发布时间】:2019-10-12 11:54:41
【问题描述】:

我正在尝试使用 npm 模块 env-cmd 在 nodejs 中的 process.env 上设置环境变量。我的系统运行的是 Kubuntu 18.04。

即使在使用 sudo 时,我也会一直收到“权限被拒绝”。

我的节点和 npm 版本来自 Ubuntu 包管理器。所以不是最新的,而是最新的“批准”的。

我想通过这个权限被拒绝的事情。

我已经尝试将 env-cmd 更新到最新的软件包并使用 -f 开关得到完全相同的结果。我已验证我的文件路径是否正确并仔细检查了我的代码。这让我觉得这是一个“linux的东西”

来自 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"
  }

来自 dev.env

PORT=3000

来自 index.js

const port = process.env.PORT;

预期结果是节点运行并在 process.env 上设置变量

终端输出是

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 ]

【问题讨论】:

    标签: javascript node.js ubuntu permissions


    【解决方案1】:

    在 Udemy 的课程助理 Adam Hartleb 的帮助下解决了这个问题。他建议用 npx 运行它,经过一番琢磨后,它奏效了。

    对于遇到此问题的其他人,这是我所做的:

    我通过检查 env-cmd 的源代码解决了这个问题,然后重新阅读了文档。

    问题在于,至少在 Ubuntu 18.04 中,env-cmd 不能很好地处理非默认文件位置。

    要让它运行,你必须有一个支持 npx 的 Node 版本。如果您在 Ubuntu 中,则必须使用 Canonical 不支持的节点版本。我解决的方法是安装 NVM 和 Node 10.15.3

    how to install NVM and Node LTS

    您需要使用 .env 文件的默认文件位置:

     ./.env
    

    您需要删除对非默认文件位置的任何引用,因为它们的存在只会激怒 env-cmd。它将有一个字面上的挂断。 (它实际上会产生一个名为“hangup”的错误)。它还将使用“spawn”一词。我只告诉你我看到了什么。

    我将“开发”脚本更改为

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

    它现在应该可以工作了。愿 .env-gods 仁慈对待您的应用。

    背景故事:我是如何到达那里的

    亚当对“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.
    

    我将问题追溯到模块代码中的一些未定义变量,我将其记录到终端。

    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
    

    在 ./.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
    

    移除非默认文件位置后,使用 env-cmd run 启动服务器:

    npx env-cmd  nodemon src/index.js
    

    【讨论】:

    • 仅供参考...... -f 命令在 Ubuntu 18.04 上对我来说就像一个魅力。看来你在同一门课上。我使用的命令'env-cmd -f ./config/dev.env node src/index.js'和env-cmd版本是env-cmd@9.0.3
    猜你喜欢
    • 2019-11-16
    • 2018-01-13
    • 2017-03-24
    • 1970-01-01
    • 2021-07-08
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多