How to fix "cannot find module'./node'' while compiling with pkg

Viewed 294

the nodejs script works when I do node main.js but when I compile it into an executable with pkg it gives me this error :

pkg/prelude/bootstrap.js:1876 throw error; ^

Error: Cannot find module './node' Require stack:

  • C:\snapshot\project\node_modules\hookcord\node_modules\snekfetch\src\index.js
  • C:\snapshot\project\node_modules\hookcord\src\fire.js
  • C:\snapshot\project\node_modules\hookcord\src\index.js
  • C:\snapshot\project\node_modules\hookcord\index.js
  • C:\snapshot\project\main.js
  1. If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath. at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function._resolveFilename (pkg/prelude/bootstrap.js:1955:46) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Module.require (node:internal/modules/cjs/loader:1005:19) at Module.require (pkg/prelude/bootstrap.js:1855:31) at require (node:internal/modules/cjs/helpers:102:18) at Object. (C:\snapshot\project\node_modules\hookcord\node_modules\snekfetch\src\index.js:3:19) at Module._compile (pkg/prelude/bootstrap.js:1930:22) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) { code: 'MODULE_NOT_FOUND', requireStack: [ 'C:\snapshot\project\node_modules\hookcord\node_modules\snekfetch\src\index.js', 'C:\snapshot\project\node_modules\hookcord\src\fire.js', 'C:\snapshot\project\node_modules\hookcord\src\index.js', 'C:\snapshot\project\node_modules\hookcord\index.js', 'C:\snapshot\project\main.js' ], pkg: true }
1 Answers

I have a similar error while packaging a NestJS application.

Here is my package.json file


{
  "bin": "dist/main.js",
  "pkg": {
    "assets": [
      "node_modules/**/*"
    ],
    "outputPath": "bin",
    "scripts": "dist/**/*.js",
    "targets": [
      "node14-linux-x64",
      "node14-macos-x64"
    ]
  },
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "build2": "pkg package.json",
    "start": "nest start --watch",
    "start:dev": "nest start --watch",
    "start:prod": "node dist/main",
  
  },
  "dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/platform-express": "^7.0.0",
    "@nestjs/schedule": "^0.4.3",
    "@nestjs/typeorm": "^7.1.4",
    "@sentry/node": "^6.7.2",
    "@sentry/tracing": "^6.7.2",
    "axios": "^0.21.1",
    "class-validator": "^0.12.2",
    "fast-speedtest-api": "^0.3.2",
    "jsonpath": "^1.1.0",
    "moment": "^2.29.1",
    "mysql": "^2.18.1",
    "mysql2": "^2.2.5",
    "querystring": "^0.2.1",
    "queue-system": "^3.0.2",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4",
    "typeorm": "^0.2.27",
    "winston": "^3.3.3",
    "xml-js": "^1.6.11",
    "xml2json": "^0.12.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.0.0",
    "@nestjs/schematics": "^7.0.0",
    "@nestjs/testing": "^7.0.0",
    "@types/express": "^4.17.3",
    "@types/node": "^13.9.1",
    "@typescript-eslint/eslint-plugin": "3.9.1",
    "@typescript-eslint/parser": "3.9.1",
    "eslint": "7.7.0",
    "eslint-plugin-import": "^2.20.1",
    "ts-loader": "^6.2.1",
    "ts-node": "9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.4"
  }
}

The command I am running is: pkg package.json and it seems to run without any error. However, when running the binary I have similar errors:

error: Error: Cannot find module 'esprima'
  Require stack:
  - aesprim
  1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
      at Function._resolveFilename (pkg/prelude/bootstrap.js:1955:46)
      at Function.resolve (internal/modules/cjs/helpers.js:107:19)
      at Object.<anonymous> (/snapshot/app/node_modules/jsonpath/lib/aesprim.js:4:20)
      at Module._compile (pkg/prelude/bootstrap.js:1930:22)
      at Object.<anonymous> (/snapshot/app/node_modules/jsonpath/lib/aesprim.js:15:11)
      at Module._compile (pkg/prelude/bootstrap.js:1930:22)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
      at Module.load (internal/modules/cjs/loader.js:950:32)
      at Function.Module._load (internal/modules/cjs/loader.js:790:12) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ 'aesprim' ],
    pkg: true
  },

I tried to install esprima manually and create the package again, but the error is the same.

Thanks for your help

Related