Welcome to the Linux Foundation Forum!

The require()'s in lab 7.2's test.js are throwing an error.

Hi, I have index.js behaving as asked in Lab 7.2 logging the sum of 19 and 23 to the console, but when I run npm test, I get the following error:

FVFZ51FEL416:labs-2 scbaker$ npm test

[email protected] test
node test.js

file:///Users/scbaker/Documents/dev/LFW211/labs/ch-7/labs-2/test.js:2
const labs1Path = require.resolve('../labs-1')
^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/scbaker/Documents/dev/LFW211/labs/ch-7/labs-2/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///Users/scbaker/Documents/dev/LFW211/labs/ch-7/labs-2/test.js:2:19
at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)

I tried changing the require()'s to imports but that doesn't work with labs1Path. I also tried changing test.js to test.cjs as it suggests, but that threw a different error.

I'm not sure if this is part of the lab challenge or if I should have completed 7.1 as CJS instead of converting it to ESM?

Answers

Categories

Upcoming Training