Welcome to the Linux Foundation Forum!

Error in lab 7.1

lcabello
lcabello Posts: 2
edited September 2022 in LFW211 Class Forum

Hi everyone,

I'm getting an error in the lab 7.1 after execute test.mjs. My code is using commonjs to export the function, where is my mistake coming from?

Error message:
_node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
^

AssertionError [ERR_ASSERTION]: function exported
at file:///Users/lcabello/projects/node/NodeJs/labs-empty/ch-7/labs-1/test.mjs:6:1 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}_

Here a simple example

//Index.js
const { add } = require('./sum');

console.log(add(1,2));

//Sum.js
'use strict';

const add = (a,b) => a + b;

module.exports = { add };

Thanks!

Answers

  • d.m.c
    d.m.c Posts: 160

    @icabello you're exporting an object with a function, you need to export a function.

  • cfinis
    cfinis Posts: 2
    edited May 19

    I have the same problem and even with the answer, I still don't get it.

    If what I'm exporting is not a function, why can I write a CJS file that can call add() as a function and claims it is a function when requiring index.js?

    $ cat cf.js
    
    op = require('./index')
    
    console.log(op.add(5,100))
    console.log(typeof op.add)
    
    $ node cf.js
    105
    function
    

    $ npm test
    > labs-1@1.0.0 test
    > node test.mjs

    node:internal/modules/run_main:122
    triggerUncaughtException(
    ^

    AssertionError [ERR_ASSERTION]: function exported
    at file:///home/cfinis/workspace/jsnad/labs.node-v22/ch-7/labs-1/test.mjs:7:1 {
    generatedMessage: false,
    code: 'ERR_ASSERTION',
    actual: false,
    expected: true,
    operator: '=='
    }

    Node.js v22.14.0

Categories

Upcoming Training