llrt: TypeError: not a function

It seems that LLRT does not load well a module (like depd) which is a collection of functions like a library.

// [depd] location: node_modules/depd/index.js
var a = require('depd') // At least it is not an error.
console.log(a);
var deprecate = require('depd')('body-parser')  // LLRT makes an error.
console.log(deprecate);

In LLRT:

% llrt ./src/reproduction.js
{}
TypeError: not a function
  at <anonymous> (src/reproduction.js:4:17)

In Node.js:

% node ./src/reproduction.js
[Function: depd]
[Function: deprecate] {
  _file: '/Users/shinya/Workspaces/hono-test/src/reproduction.js',
  _ignored: false,
  _namespace: 'body-parser',
  _traced: false,
  _warned: [Object: null prototype] {},
  function: [Function: wrapfunction],
  property: [Function: wrapproperty]
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 16 (5 by maintainers)

Most upvoted comments

@richarddavison I have just tried 0.3.0 with the Opensearch package, and I still get this error from the debug package it depends on.

2024-10-25T21:42:44.024Z	n/a	FATAL	TypeError: not a function
  at node_modules/debug/src/node.js (/var/task/index.js:644:28)
      at __require2 (/var/task/index.js:17:18)
      at node_modules/debug/src/index.js (/var/task/index.js:809:24)
      at __require2 (/var/task/index.js:17:18)
      at node_modules/@opensearch-project/opensearch/lib/Client.js (/var/task/index.js:11265:17)
      at __require2 (/var/task/index.js:17:18)
      at node_modules/@opensearch-project/opensearch/index.js (/var/task/index.js:11516:31)
      at __require2 (/var/task/index.js:17:18)
      at <anonymous> (/var/task/index.js:12183:28)
INIT_REPORT Init Duration: 82.13 ms	Phase: init	Status: error	Error Type: Runtime.ExitError
2024-10-25T21:42:44.122Z	n/a	FATAL	TypeError: not a function
  at node_modules/debug/src/node.js (/var/task/index.js:644:28)
      at __require2 (/var/task/index.js:17:18)
      at node_modules/debug/src/index.js (/var/task/index.js:809:24)
      at __require2 (/var/task/index.js:17:18)
      at node_modules/@opensearch-project/opensearch/lib/Client.js (/var/task/index.js:11265:17)
      at __require2 (/var/task/index.js:17:18)
      at node_modules/@opensearch-project/opensearch/index.js (/var/task/index.js:11516:31)
      at __require2 (/var/task/index.js:17:18)
      at <anonymous> (/var/task/index.js:12183:28)
INIT_REPORT Init Duration: 78.30 ms	Phase: invoke	Status: error	Error Type: Runtime.ExitError

I suspect this is due to missing assert module. This error: TypeError: not a function means whatever you’re trying to execute is either not implemented or is undefined 😃 Try stubbing assert and see:

globalThis.assert = (...args) => {
console.log("assert called with", args)
}

@richarddavison am i correct to assume this fix is in the latest release?

Yes! We however have two more (https://github.com/awslabs/llrt/issues/642 and https://github.com/awslabs/llrt/issues/641) CJS related bugs. One of them we have a patch for. The other is not to complex either. Bridging CJS and MJS seamlessly is very complicated but super useful for the end users 😃

Found the issue, it was a pretty simple fix! Functions are also Objects which we exported properties of. Now we only do so for only “real” objects. Everything else gets exported as a value.

The reproduced code for cases 1 and 2 is as follows.

// require1.js
module.exports = function require1() {
    return true;
};
// require2.js
module.exports = function require2() {
  function hello (message) {
    console.log("Hello," + message);
  }
  return hello
}

And if we call these as follows, we can see the difference.

% cat ./src/reproduction.js
const require1 = require('./require1.js')(); console.log(require1);

% llrt ./src/reproduction.js 
TypeError: not a function
  at <anonymous> (/Users/shinya/Workspaces/hono-test/./src/reproduction.js:1:18)
      at <anonymous> (eval_script:1:5)

% node ./src/reproduction.js
true
% cat ./src/reproduction.js
const require2 = require('./require2.js')('hello'); require2('LLRT!');

% llrt ./src/reproduction.js
TypeError: not a function
  at <anonymous> (/Users/shinya/Workspaces/hono-test/./src/reproduction.js:1:18)
      at <anonymous> (eval_script:1:5)

% node ./src/reproduction.js
Hello,LLRT!

Case 3 cannot be reproduced well, so I will provide reproduction code after a little more research on the actual use case.

Can you create a simple reproducable? This seems like a bug

The following statement will likewise result in an error.

// node_modules/aws-sdk/lib/core.js
AWS.util.update(AWS, {
  ...
  EndpointCache: require('../vendor/endpoint-cache').EndpointCache   // TypeError: not a function
});

This is SDK v1 no? Its not supported by LLRT