简体   繁体   English

如何在使用 pkg 编译时修复“找不到模块'./node''

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

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 : nodejs 脚本在我执行 node main.js 时有效,但是当我使用 pkg 将其编译为可执行文件时,它给了我这个错误:

pkg/prelude/bootstrap.js:1876 throw error; pkg/prelude/bootstrap.js:1876 抛出错误; ^ ^

Error: Cannot find module './node' Require stack:错误:找不到模块 './node' 需要堆栈:

  • C:\snapshot\project\node_modules\hookcord\node_modules\snekfetch\src\index.js 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\fire.js
  • C:\snapshot\project\node_modules\hookcord\src\index.js C:\snapshot\project\node_modules\hookcord\src\index.js
  • C:\snapshot\project\node_modules\hookcord\index.js C:\snapshot\project\node_modules\hookcord\index.js
  • C:\snapshot\project\main.js C:\快照\项目\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.如果要将包/文件编译为可执行文件,请注意编译警告并在“require”调用中指定文字。 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. 2) 如果您不想将包/文件编译为可执行文件并希望从文件系统(可能是插件)中“要求”它,请使用 process.cwd() 或 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.在 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) 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 } (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 }

I have a similar error while packaging a NestJS application.我在打包 NestJS 应用程序时遇到了类似的错误。

Here is my package.json file这是我的 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"
  }
}

The command I am running is: pkg package.json and it seems to run without any error.我正在运行的命令是: pkg package.json ,它似乎运行没有任何错误。 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.我尝试手动安装 esprima 并再次创建包,但错误是一样的。

Thanks for your help谢谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM