Welcome to the Linux Foundation Forum!

Why can't I get chapter 7 code to run?

srexi
srexi Posts: 2
edited September 4 in LFW211 Class Forum

I'm trying to follow along in Chapter 7 "Converting a CJS Package to an ESM Package (1)". I'm running the commands along the tutorial to let stuff sink in, but I can't get 'npm start' to run with the source code I have.

I attached the source and the debug log.

Answers

  • It happened to me also.

    I changed the code and it worked. Here is the code:

    import * as format from './format.js'
    
    const isMain = process.argv[1]
    
    if (isMain) {
        const {
            default: pino
        } = await import('pino')
        const logger = pino()
        logger.info(format.upper('my-package started'))
        process.stdin.resume()
    }
    
    export default (str) => {
        return format.upper(str).split('').reverse().join('')
    }
    
  • I found the error without removing those lines (url. preceding to fileURLToPath):

    import { realpath } from 'fs/promises'
    import url from 'url'
    import * as format from './format.js'
    
    const isMain = process.argv[1] &&
     await realpath(url.fileURLToPath(import.meta.url)) ===
     await realpath(process.argv[1])
    
    if (isMain) {
      const { default: pino } = await import('pino')
      const logger = pino()
      logger.info(format.upper('my-package started'))
      process.stdin.resume()
    }
    
    export default (str) => {
      return format.upper(str).split('').reverse().join('')
    }
    
  • srexi
    srexi Posts: 2

    That did it. Good eye. Thank you guillermotti

  • xdxmxc
    xdxmxc Posts: 61
    edited November 7

    thanks @guillermotti - good catch, material is being updated (just going to do import { fileURLToPath } from 'url' since url is only imported for that function)

  • fcioanca
    fcioanca Posts: 1,453

    This has been corrected. Thank you!

This discussion has been closed.

Categories

Upcoming Training