Welcome to the Linux Foundation Forum!

Error in lab 7.1

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

  • Posts: 160

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

  • 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?

    1. $ cat cf.js
    2.  
    3. op = require('./index')
    4.  
    5. console.log(op.add(5,100))
    6. console.log(typeof op.add)
    7.  
    8. $ node cf.js
    9. 105
    10. 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

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training