Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] npm-run runs in background if it cannot write logs #6520

Closed
2 tasks done
blaise-io opened this issue Jun 2, 2023 · 2 comments
Closed
2 tasks done

[BUG] npm-run runs in background if it cannot write logs #6520

blaise-io opened this issue Jun 2, 2023 · 2 comments
Assignees
Labels
Bug thing that needs fixing Release 9.x work is associated with a specific npm 9 release

Comments

@blaise-io
Copy link

blaise-io commented Jun 2, 2023

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

When I run this command

$ sudo -u nobody sh -c 'npm run server'

I get the following output:

> test@1.0.0 server
> node index.js

Server running at http://127.0.0.1:3000/
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /nonexistent
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/nonexistent'
npm ERR!  [Error: EACCES: permission denied, mkdir '/nonexistent'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/nonexistent'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! Log files were not written due to an error writing to the directory: /nonexistent/.npm/_logs
npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal

At this point my server is still running, as a background process. (as if I ran the command with &, but I did not.)

$ curl http://127.0.0.1:3000/
Hello World

I know I can fix the EACCES by making sure the path is writable using sudo -u nobody sh -c 'npm run server --cache .', my problem is that npm does not handle the error in an expected way.

Expected Behavior

I would expect that either

A) The server stops and the process exits with an error code due to the errors being fatal, OR
B) The process runs in the foreground ignoring non-fatal errors.

Steps To Reproduce

// index.js
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "server": "node index.js"
  },
  "author": "Blaise",
  "license": "ISC"
}

Now run sudo -u nobody sh -c 'npm run server', where -u nobody causes npm not to be able to write logs.

Environment

  • npm: 9.6.6
  • Node.js: v20.2.0
  • OS Name: Ubuntu 22.04.2 LTS (jammy)
  • System Model Name: AWS EC2 / also on MacBook Air 2020 with Docker
  • npm config:
; node bin location = /usr/bin/node
; node version = v20.2.0
; npm local prefix = /app
; npm version = 9.6.6
; cwd = /app
; HOME = /root
; Run `npm config ls -l` to show all defaults.
@blaise-io blaise-io added Bug thing that needs fixing Needs Triage needs review for next steps Release 9.x work is associated with a specific npm 9 release labels Jun 2, 2023
@myleshk
Copy link

myleshk commented Aug 29, 2023

+1 and looks duplicate of #6245

@lukekarrys lukekarrys self-assigned this May 11, 2024
@lukekarrys lukekarrys removed the Needs Triage needs review for next steps label May 14, 2024
@lukekarrys
Copy link
Member

This was fixed by c7a592c as part of npm@10.2.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Release 9.x work is associated with a specific npm 9 release
Projects
None yet
Development

No branches or pull requests

3 participants