Welcome to the Linux Foundation Forum!

resolve-demo.cjs

I'm getting errors in the code for the resolve-demo.cjs file. I've copied the code from the course precisely and am getting errors on lines 19/20 and 26/27 on the require portion that follows console.log ... has anyone found a way to resolve this?? I'm also getting an error found on the very last line (30 on my VS) after the console.log()
Also, the ` is preceding every require following console log but when I save the code on here for review it doesn't show it. Just wanted to add this so there isn't a misconception that I left them out

Please help

Here's the full code for reference:

'use strict'

console.log()
console.group('# package resolution')
console.log(require(pino), '\t', '=>',
require.resolve('pino'))
console.log(require('standard'), '\t', ' =>',
require.resolve('standard'))
console.groupEnd('')
console.log()

console.group('# directory resolution')
console.log(require('.'), '\t\t', '=>', require.resolve('.'))
console.log(`require('../my-package'))
console.groupEnd()
console.log

console.group('# file resolution')
console.log(require('./format'), '\t', ' =>', require.resolve('./format'))
console.log(require('./format.js'), ' =>', require.resolve('./format.js'))
require.resolve('./format.js'))
console.groupEnd()
console.log()

console.group('# core APIs resolution')
console.log(require('fs'), '\t', ' =>', require.resolve('fs'))
console.log(require('util'), '\t', ' =>', require.resolve('util'))
require.resolve('util))
console.groupEnd()
console.log()

Best Answer

  • debido
    debido Posts: 4
    Answer ✓

    to paste a block of code into the code editor, precede the block with three backticks and end the block with three backticks. any code less than a (multiline) block, use a single backtick at the beginning and end. that should preserve your code in posts.

    after a glance, the problem may be one too many right parentheses at the end of a few statements, and extra require.resolves. if you copied it exactly as is, the source may be corrupted.

    check the syntax of your version of resolve-demo with node --check resolve-demo.cjs. if there are syntax errors, that should tell you where they are.

    i copy-pasted code i had from that lesson below for your reference. it runs with node resolve-demo.cjs without error on my machine. it likely differs from your setup, but can serve as a reference if you need it.

    if i remember correctly, the point of this part of the lesson is just to see how require.resolve works by printing out absolute paths for packages, directories, files, and core api's. you could achieve that result without the extra logging by using a single statement, like console.log(require.resolve('./relative-path')).

    :smile:

    'use strict'
    
    console.log()
    console.group('# package resolution')
    console.log(`require('pino')`, '\t', ' =>', require.resolve('pino'))
    console.log(`require('standard')`, '\t', ' =>', require.resolve('standard'))
    console.groupEnd('')
    console.log()
    
    console.group('# directory resolution')
    console.log(`require('.')`, '\t\t', ' =>', require.resolve('.'))
    console.log(`require('../muh-package')`, '=>', require.resolve('../muh-package'))
    console.groupEnd()
    console.log()
    
    console.group('# file resolution')
    console.log(`require('./format')`, '\t', ' =>', require.resolve('./format'))
    console.log(`require('./format.js')`, ' =>', require.resolve('./format.js'))
    console.groupEnd()
    console.log()
    
    console.group('# core APIs resolution')
    console.log(`require('fs')`, '\t', ' =>', require.resolve('fs'))
    console.log(`require('util')`, '\t', ' =>', require.resolve('util'))
    console.groupEnd()
    console.log()
    

Answers

Categories

Upcoming Training