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

Problem reading module.parent.filename when using commonjs #88

Open
vadymrybak opened this issue Jul 13, 2022 · 2 comments
Open

Problem reading module.parent.filename when using commonjs #88

vadymrybak opened this issue Jul 13, 2022 · 2 comments

Comments

@vadymrybak
Copy link

While running the app locally (npm run dev) - everything is fine. Package.json below:

{
  "name": "workflow-mfe-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
    "build:prod": "webpack --env prod",
    "build:dev": "webpack --env dev",
    "prestart": "npm run prebuild",
    "start": "ts-node -r ./src/tracer.ts ./src/index.ts",
    "dev": "nodemon",
    "test": "jest --verbose=true --coverage --forceExit"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@opentelemetry/api": "^1.0.3",
    "@opentelemetry/exporter-collector-grpc": "^0.24.0",
    "@opentelemetry/instrumentation": "^0.24.0",
    "@opentelemetry/instrumentation-express": "^0.25.0",
    "@opentelemetry/instrumentation-http": "^0.24.0",
    "@opentelemetry/sdk-node": "^0.24.0",
    "axios": "^0.27.2",
    "chalk": "^4.1.0",
    "cookie-parser": "^1.4.6",
    "crypto": "^1.0.1",
    "dotenv-flow": "^3.1.0",
    "express": "^4.17.1",
    "express-http-context": "^1.2.4",
    "jwt-decode": "^3.1.2",
    "licensing-lib-js": "^1.0.14",
    "logform": "^2.2.0",
    "prom-client": "^14.0.1",
    "prometheus-api-metrics": "^3.2.2",
    "uuid": "^8.3.2",
    "winston": "^3.3.3"
  },
  "devDependencies": {
    "@types/cookie-parser": "^1.4.2",
    "@types/dotenv-flow": "^3.2.0",
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.21",
    "@types/logform": "^1.10.1",
    "@types/supertest": "^2.0.11",
    "@types/uuid": "^8.3.1",
    "@types/winston": "^2.4.4",
    "dotenv": "^8.2.0",
    "jest": "^26.6.3",
    "supertest": "^6.1.6",
    "ts-jest": "^26.5.6",
    "ts-loader": "^8.0.17",
    "ts-node": "^10.8.2",
    "typescript": "^4.2.3",
    "webpack": "^5.24.2",
    "webpack-cli": "^4.5.0"
  },
  "nodemonConfig": {
    "ignore": [
      "**/*.test.ts",
      "**/*.spec.ts",
      ".git",
      "node_modules",
      "dist"
    ],
    "watch": [
      "src"
    ],
    "exec": "npm start",
    "ext": "ts"
  }
}

Tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es5",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*"
            ]
        }
    },
    "include": [
        "src/**/*"
    ]
}

The problem occurs when I build production app. When trying to launch the server by calling "node index.js" - I'm getting error:

Error: Could not find package.json up from 53263

By digging some source code I found this line of code in index.js of prometheus-api-metrics:

require('pkginfo')(metricsMiddleware, { dir: Path.dirname(module.parent.filename), include: ['name', 'version'] });

Apparently, when using "commonjs" - module.parent does not exist and it is causing a problem.

Does anyone have any solution for this?

@natl-set
Copy link

natl-set commented Jul 7, 2023

I ran into the same problem

@RubenNL
Copy link
Contributor

RubenNL commented Jul 10, 2024

By patching that file like this:

-const metricsMiddleware = { exports: {} };
-require('pkginfo')(metricsMiddleware, { dir: Path.dirname(module.parent.filename), include: ['name', 'version'] });
+const metricsMiddleware = { exports: {
+  name: "dummy",
+  version: "0.0.0"
+} };

All the features I need work again. I can't find the "dummy" name anywhere, so I guess it's just ignored?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants