0

I'm trying to recreate the migrate function in this codebase: https://codesandbox.io/s/e9fykv?file=/migrations/migrate.ts.

The relevant files in that codebase, for this question, are the following: "migrate.ts" and "node-pg-migrate" in the Migrations Folder (as well as the files and modules they reference).

I'm recreating this in NodeJS (not typescript), and have almost everything set up, but the core error I'm getting now is an ENOENT error when running the migration function, specifically during the child_process spawn call that references the "node-pg-migrate" file.

It appears that the error is that I cannot even reference the file for the spawn call -- so I haven't even encountered any internal errors for the code itself in that file yet.

Here's my "migrate.js" code: migrate.js

import { spawn } from 'child_process';
import * as path from 'path';
import dotenv from 'dotenv';
import * as url from 'node:url';

dotenv.config();

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

export function migrate(commandArguments, migrationsDir = __dirname)
{

    const spawnArgs = ['--migrations-dir',
        migrationsDir,
        ...commandArguments
    ];

    console.log('Running command:');
    console.dir(`node-pg-migrate ${spawnArgs.join(' ')}`);

    console.log(`Spawn Path: \n${path.resolve(migrationsDir, 'node-pg-migrate')}\n`);

    const child = spawn(
        path.resolve(migrationsDir, 'node-pg-migrate'),
        spawnArgs,
        {
            env: { ...process.env },
            windowsVerbatimArguments: false
        }
    );

    child.on('error', function (error) {
        console.log(error.message);
    });

    child.stdout.on('data', function (data) {
        console.log('stdout output: ');
        console.log(data.toString());
    });

    child.stderr.on('data', function (data) {
        console.log('stderr output: ');
        console.log(data.toString());
    });

    child.on('close', (code, signal) => {
        console.log(`child process exited with code: ${code} and signal: ${signal}`);
    });

    return child;
}

//CJS Check
// if (require.main === module) {
//     migrate(process.argv.slice(2), path.resolve(__dirname, '../migrations'))
// }

//ES6 Main Check
if (import.meta.url.startsWith('file:')) {
    const modulePath = url.fileURLToPath(import.meta.url);
    if (process.argv[1] === modulePath) {
        migrate(process.argv.slice(2), path.resolve(__dirname, '../migrations'));
    }
}

and here is my node-pg-migrate code: node-pg-migrate

#!usr/bin/env node

require('../config/nodeconfig.json')
require('../node_modules/node-pg-migrate/bin/node-pg-migrate')

Here is the output from running this migrate function (script for migrate is "migrate: node ./migrations/migrate.js"):

(in terminal -- powershell): npm run migrate up

Output:

npm WARN config global --global, --local are deprecated. Use --location=global instead.

desktop-server@1.0.0 migrate node ./migrations/migrate.js "up"

Running command: 'node-pg-migrate --migrations-dir C:\Users\M\NodeJSProjects\xxxx\Prototype2\desktop-server\migrations up' Spawn Path: C:\Users\M\NodeJSProjects\xxxx\Prototype2\desktop-server\migrations\node-pg-migrate

spawn C:\Users\M\NodeJSProjects\xxxx\Prototype2\desktop-server\migrations\node-pg-migrate ENOENT child process exited with code: -4058 and signal: null

For further information, I've tried setting windowsVerbatimArguments to true (same result), commenting out every line in node-pg-migrate, and setting a $PATH variable to the absolute path location of the node-pg-migrate file.

Any idea what's causing this?

mpr4583
  • 1
  • 1

1 Answers1

0

ENOENT usually indicates that you did not set the spawn child process working directory (by default the child process inherits the current working directory).

You can set the child process working directory by adding cwd: migrationsDir to spawn function options next to env

TBA
  • 601
  • 5
  • 6