winstonjs / winston

A logger for just about everything.
http://github.com/winstonjs/winston
MIT License
22.97k stars 1.82k forks source link

Packaged Electron app throws Cannot find module error #1926

Open mrr0b0t-0x1 opened 3 years ago

mrr0b0t-0x1 commented 3 years ago

Please tell us about your environment:

What is the problem?

I am using winston logger in an Electron app, when I run the app with npm start, the logger works just fine without any errors. But when I package my app with any packager like electron-packager or electron-forge, it gives me an error as below -

Uncaught Error: Cannot find module '../adapters/process.env'
Require stack:
- C:\Users\path-to-pkg\resources\app\node_modules\@dabh\diagnostics\node\development.js
- C:\Users\path-to-pkg\resources\app\node_modules\@dabh\diagnostics\node\index.js
- C:\Users\path-to-pkg\resources\app\node_modules\winston\lib\winston\exception-handler.js
- C:\Users\path-to-pkg\resources\app\node_modules\winston\lib\winston\logger.js
- C:\Users\path-to-pkg\resources\app\node_modules\winston\lib\winston\create-logger.js
- C:\Users\path-to-pkg\resources\app\node_modules\winston\lib\winston.js
- C:\Users\path-to-pkg\resources\app\js\log_dat.js
- C:\Users\path-to-pkg\resources\app\index.html
    at Module._resolveFilename (internal/modules/cjs/loader.js:798)
    at Function../lib/common/reset-search-paths.ts.Module._resolveFilename (reset-search-paths.ts:42)
    at Module._load (internal/modules/cjs/loader.js:691)
    at Function.Module._load (electron/js2c/asar.js:748)
    at Module.require (internal/modules/cjs/loader.js:853)
    at require (internal/modules/cjs/helpers.js:74)
    at Object.<anonymous> (C:\Users\path-to-pkg\resources\app\node_modules\@dabh\diagnostics\node\development.js:30)
    at Object.<anonymous> (C:\Users\path-to-pkg\resources\app\node_modules\@dabh\diagnostics\node\development.js:38)
    at Module._compile (internal/modules/cjs/loader.js:968)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986)

What do you expect to happen instead?

The logger should work just fine as it does before packaging, this only happens when the app is run as packaged.

Other information

I don't know what else I should look for, I tried

but nothing has worked so far.

This is my log_dat.js file -

const path = require('path')
const { createLogger, format, transports } = require('winston');

// Create logger
const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    format.errors({ stack: true }),
    format.splat(),
    format.printf(info => `${[info.timestamp]} - ${info.label} - ${info.level.toUpperCase()} - ${info.message}`),
    format.align()
  ),
  defaultMeta: { service: 'user-service' },
  transports: [
    new transports.File({ 
      filename: path.resolve(__dirname, '..', 'logs', 'node-error.log'),
      level: 'error',
      maxsize: 100000,
      maxFiles: 5,
      tailable: true
    }),
    new transports.File({ 
      filename: path.resolve(__dirname, '..', 'logs', 'node-combined.log'),
      maxsize: 100000,
      maxFiles: 10,
      tailable: true
    })
  ],
  exitOnError: false
})

module.exports = {
  logger: logger
}
mrr0b0t-0x1 commented 3 years ago

I took a closer look at how the app was getting packaged, so I started from the package.json file which first got modified when I imported my project with electron-forge. So I looked at the config section and found that electron-forge was ignoring a bunch of files and directories. It included this glob pattern ".*.env", after removing this, now everything is working all good. So I guess it ignored some .env files that are required by winston to run.

But I would like to know, will removing this glob pattern ".*.env" expose some sensitive .env files in the project?

Or how could I just allow the .env files required by winston and ignore everything else?

Aanandgupta commented 3 years ago

Hello @mrr0b0t-0x1 i am also getting error as ../../connect not found i am using this import in elrctron.js when i am using npm run watch every thing works fine but when i run .exe file this error occurs

DanielChrome commented 1 year ago

Hi. I'm receiving the same error but is for "logform". Running in dev mode is everything OK. after build and run EXE, the errors occurs.

My Deps:

"dependencies": {
    "@quasar/extras": "^1.15.5",
    "@stripe/terminal-js": "^0.11.2",
    "axios": "^1.1.3",
    "concurrently": "^7.6.0",
    "mitt": "^3.0.0",
    "moment": "^2.29.4",
    "path": "^0.12.7",
    "quasar": "^2.10.2",
    "sass": "1.32.12",
    "simple-keyboard": "^3.4.184",
    "toad-scheduler": "^2.0.0",
    "uuid": "^9.0.0",
    "vue": "^3.2.43",
    "vue-router": "^4.1.6",
    "winston": "^3.10.0",
    "winston-daily-rotate-file": "^4.7.1",
    "winston-transport": "^4.5.0",
    "zod": "^3.21.4"
  },

I tried install logform as dependency and the error change to "Coul'd not find module '@colours/color/safe'.

image

Any Help?

ing-end commented 10 months ago

Hi. I'm getting the same error. When I use the "cnpm install" install command, then the error appears. When I'm using "yarn", The error disappeared.

TuKun33 commented 4 months ago

Copy and paste your module from devDependenciesto dependencies

wogno commented 3 weeks ago

Hi, someone found solution ?