簡體   English   中英

如何在使用 pkg 編譯時修復“找不到模塊'./node''

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

nodejs 腳本在我執行 node main.js 時有效,但是當我使用 pkg 將其編譯為可執行文件時,它給了我這個錯誤:

pkg/prelude/bootstrap.js:1876 拋出錯誤; ^

錯誤:找不到模塊 './node' 需要堆棧:

  • 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:\快照\項目\main.js
  1. 如果要將包/文件編譯為可執行文件,請注意編譯警告並在“require”調用中指定文字。 2) 如果您不想將包/文件編譯為可執行文件並希望從文件系統(可能是插件)中“要求”它,請使用 process.cwd() 或 process.execPath 在“要求”調用中指定絕對路徑。 在 Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) 在 Function._resolveFilename (pkg/prelude/bootstrap.js:1955:46) 在 Function.Module._load (node:internal/modules /cjs/loader:778:27) 在 Module.require (node:internal/modules/cjs/loader:1005:19) 在 Module.require (pkg/prelude/bootstrap.js:1855:31) 在 require (node:內部/模塊/cjs/helpers:102:18)在對象。 (C:\snapshot\project\node_modules\hookcord\node_modules\snekfetch\src\index.js:3:19) 在 Module._compile (pkg/prelude/bootstrap.js:1930:22) 在 Object.Module._extensions。 .js (node:internal/modules/cjs/loader:1159:10) 在 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 }

我在打包 NestJS 應用程序時遇到了類似的錯誤。

這是我的 package.json 文件


{
  "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"
  }
}

我正在運行的命令是: pkg package.json ,它似乎運行沒有任何錯誤。 但是,在運行二進制文件時,我遇到了類似的錯誤:

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
  },

我嘗試手動安裝 esprima 並再次創建包,但錯誤是一樣的。

謝謝你的幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM