child_process.execSync 执行 npm 运行 脚本找不到 package.json

child_process.execSync executing a npm run script cant find the package.json

我正在尝试在 child_process 中执行 cypress 并将一些对象传递给规范以执行。我已经尝试使用 execSync 来 运行 npm 运行 脚本,并在 运行 将脚本 npm link 然后 npm install -g 像 cli 应用程序一样使用它。我得到这个错误

npm ERR! syscall open
npm ERR! path /home/mohibulhasan/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/mohibulhasan/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/mohibulhasan/.npm/_logs/2022-01-28T07_38_15_104Z-debug.log
child_process.js:669
    throw err;
    ^

Error: Command failed: cd /home/mohibulhasan && npm run cypress-run -- --env reportInfo='{"projectName":"Project Name"}'
    at checkExecSyncError (child_process.js:630:11)
    at execSync (child_process.js:666:15)
    at Object.<anonymous> (/home/mohibulhasan/Documents/erp/app.js:33:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  status: 254,
  signal: null,
  output: [ null, null, null ],
  pid: 286825,
  stdout: null,
  stderr: null
}

我尝试使用 path 给出当前文件路径以获取 package.json 但找不到它。然后也尝试使用 npm pack 然后安装它,但这也导致了同样的错误。我错过了什么?

package.json

{
  "name": "erp",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "cypress-run": "cypress run --headed 'cypress/integration/todo.spec.js'"
  },
  "bin": {
    "erp-report": "./app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chalk": "^4.0.0",
    "commander": "^8.3.0",
    "cypress": "^9.3.1"
  }
}

app.js

#!/usr/bin/env node

const chalk = require('chalk')
const {Command} = require('commander')
const {execSync} = require('child_process')
const path = require('path')
const packgejson = require('./package.json');

const program = new Command();

const greeting = chalk.green.bold("Hello!");

console.log(greeting);
console.log(packgejson)

program
.option('-p, --project-name <type>', 'insert project name')
.option('-t, --task-name <type>', 'insert task name')
.option('-s, --start-time <type>', 'insert start time')
.option('-e, --end-time <type>', 'insert end time')
.option('-h, --work-hour <type>', 'insert work hour')
.option('-r, --report <type>', 'insert report message')
program.parse(process.argv);

const options = program.opts();

console.log(options.projectName)
console.log(options.taskName)
const reportInfo = JSON.stringify(options);
const currentFolderPath = path.resolve(process.cwd());
console.log(currentFolderPath)

execSync(`cd ${currentFolderPath} && npm run cypress-run -- --env reportInfo='${reportInfo}'`, {stdio: 'inherit'})

您好,当前工作目录可能指向您的项目目录。

改用__dirname

const chalk = require('chalk')
const {Command} = require('commander')
const {execSync} = require('child_process')
const path = require('path')
const packgejson = require('./package.json');

const program = new Command();

const greeting = chalk.green.bold("Hello!");

console.log(greeting);
console.log(packgejson)

program
.option('-p, --project-name <type>', 'insert project name')
.option('-t, --task-name <type>', 'insert task name')
.option('-s, --start-time <type>', 'insert start time')
.option('-e, --end-time <type>', 'insert end time')
.option('-h, --work-hour <type>', 'insert work hour')
.option('-r, --report <type>', 'insert report message')
program.parse(process.argv);

const options = program.opts();

console.log(options.projectName)
console.log(options.taskName)
const reportInfo = JSON.stringify(options);
const currentFolderPath = __dirname;
console.log(currentFolderPath)

execSync(`cd ${currentFolderPath} && npm run cypress-run -- --env reportInfo='${reportInfo}'`, {stdio: 'inherit'})