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

Time:01-28

I am trying to execute cypress in a child_process and pass some object to the spec to execute. I have tried it using execSync to run the npm run script and pass the object as env variable.But when running the script by npm link and then npm install -g to use it like a cli app. i get this error

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
}

I tried using the path to give the current files path to get package.json but cant find it. Then also tried using npm pack and then install it, but that also resulted with the same error. i am missing something?

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'})

CodePudding user response:

Hi current working directory may or may point to your project directory.

use __dirname instead :

i.e.

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'})
  •  Tags:  
  • Related