sainsburys-tech / next-logger

JSON logging patcher for Next.js
MIT License
144 stars 14 forks source link

Not able to start when add next-logger #6

Closed dkleber89 closed 2 years ago

dkleber89 commented 2 years ago

When i add next-logger to my Project im not able to start nextjs anymore:

> cross-env NODE_OPTIONS='-r next-logger' next start

Der Befehl "next-logger" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
events.js:352
      throw er; // Unhandled 'error' event
      ^

Error: spawn next-logger ENOENT
    at notFoundError (C:\Projekte\Mobility.Portal.Web\node_modules\cross-spawn\lib\enoent.js:6:26)
    at verifyENOENT (C:\Projekte\Mobility.Portal.Web\node_modules\cross-spawn\lib\enoent.js:40:16)
    at ChildProcess.cp.emit (C:\Projekte\Mobility.Portal.Web\node_modules\cross-spawn\lib\enoent.js:27:25)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess.cp.emit (C:\Projekte\Mobility.Portal.Web\node_modules\cross-spawn\lib\enoent.js:30:37)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12) {
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn next-logger',
  path: 'next-logger',
  spawnargs: [ 'next', 'start' ]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mobility.portal.web@1.0.0 start: `cross-env NODE_OPTIONS='-r next-logger' next start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mobility.portal.web@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Projekte\_npm\_logs\2021-09-21T07_33_23_675Z-debug.log

Used Versions: "next": "^11.1.2", "next-logger": "^2.1.1",

OS: Windows for dev and later Alpine Linux Kubernetes Container

What do im wrong?

dkleber89 commented 2 years ago

When i use the same in Alpine Linux Container it works like expected

atkinchris commented 2 years ago

Thanks for raising this - I don't develop natively on Windows (always through WSL or macOS), so I hadn't run into it before.

To rule out interference from cross-env or cross-spawn, please can you see if the following works for you as a test?

set NODE_OPTIONS="-r next-logger"

npx next start
# ...or whichever script you have aliases to "next start"
# e.g. npm run start
dkleber89 commented 2 years ago

When i set NODE_OPTIONS directly then it works as expected.

Maybe there is a Problem with cross-env ... but can´t find out what it is ... the other env variables working fine with the cross-env

cahaseler commented 2 years ago

I'm also interested in using this to develop on Windows without having to ask my team to manually set config vars. Happy to help test if you have ideas.

atkinchris commented 2 years ago

@cahaseler - the library needs to be loaded into the Node environment before any other code, including Next.js. The fastest way is to use NODE_OPTIONS to require a module into the environment before starting Next.js' CLI - and environment variables are needed for this.

Alternatively, you could write a tiny script that requires next-logger and then the Next.js' CLI - and then run this, instead of the Next.js CLI. For example:

require('next-logger')
require('next/dist/bin/next')
node next.js
laggingreflex commented 2 years ago

This seems to be an issue with cross-env: #143, #248.

PS: I've just fixed this in my library https://github.com/laggingreflex/win-node-env/issues/8 which is an alternative to cross-env.

atkinchris commented 2 years ago

I'm going to close this, as an issue with cross-env. If it turns out to be this library, please re-open this.