Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fail due to fs.exists TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined #78

Open
lancegliser opened this issue Nov 30, 2022 · 0 comments

Comments

@lancegliser
Copy link
Contributor

lancegliser commented Nov 30, 2022

On the master branch, using the commands README:

docker run -it --rm \
  --name=dynamodb-test \
  -p 127.0.0.1:8000:8000 \
  deangiberson/aws-dynamodb-local
export AWS_CONFIG_JSON='{"endpoint": "http://127.0.0.1:8000", "region": "us-east-1", "accessKeyId": "accesskey", "secretAccessKey": "secretaccesskey"}'
npm test

I run into this error:

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at maybeCallback (node:fs:177:3)
    at Object.exists (node:fs:260:3)
    at Object.<anonymous> (/Users/lancegliser/Projects/lancegliser/connect-dynamodb-v2-dependency/test/test.js:9:17)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async formattedImport (/Users/lancegliser/Projects/lancegliser/connect-dynamodb-v2-dependency/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/Users/lancegliser/Projects/lancegliser/connect-dynamodb-v2-dependency/node_modules/mocha/lib/nodejs/esm-utils.js:38:28)
    at async Object.exports.loadFilesAsync (/Users/lancegliser/Projects/lancegliser/connect-dynamodb-v2-dependency/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/Users/lancegliser/Projects/lancegliser/connect-dynamodb-v2-dependency/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/lancegliser/Projects/lancegliser/connect-dynamodb-v2-dependency/node_modules/mocha/lib/cli/run.js:370:5)

test/test.js:9:17 is:

var fs = require('fs'),
// Other stuff
var config = fs.exists('./aws-config.json') && fs.readFileSync('./aws-config.json');

The definition of fs.exists reads:

deprecated export function exists( path: PathLike, callback: (exists: boolean) => void): void
Test whether or not the given path exists by checking with the file system. Then call the callback argument with either true or false:
import { exists } from 'fs';

exists('/etc/passwd', (e) => {
console.log(e ? 'it exists' : 'no passwd!');
});
The parameters for this callback are not consistent with other Node.js callbacks. Normally, the first parameter to a Node.js callback is an errparameter, optionally followed by other parameters. The fs.exists() callback has only one boolean parameter. This is one reason fs.access() is recommended instead of fs.exists().
Using fs.exists() to check for the existence of a file before callingfs.open(), fs.readFile() or fs.writeFile() is not recommended. > Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file does not exist.

If I update the test config file read to the follow it works:

let config;
try {
    config = fs.readFileSync('./aws-config.json')
} catch {}
if (config) {
    client = new AWS.DynamoDB(JSON.parse(config));
}
else if (process.env.AWS_CONFIG_JSON) {
    var AWS = require('aws-sdk');
    config = JSON.parse(process.env.AWS_CONFIG_JSON);
    client = new AWS.DynamoDB(config);
}

Not sure I care to fix it, as I'm looking a different configuration in #76 but thought I'd document the issue and solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant