Welcome to the Linux Foundation Forum!

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

Posts: 2
edited September 2022 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.

Welcome!

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

Answers

  • It happened to me also.

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

    1. import * as format from './format.js'
    2.  
    3. const isMain = process.argv[1]
    4.  
    5. if (isMain) {
    6. const {
    7. default: pino
    8. } = await import('pino')
    9. const logger = pino()
    10. logger.info(format.upper('my-package started'))
    11. process.stdin.resume()
    12. }
    13.  
    14. export default (str) => {
    15. return format.upper(str).split('').reverse().join('')
    16. }
  • I found the error without removing those lines (url. preceding to fileURLToPath):

    1. import { realpath } from 'fs/promises'
    2. import url from 'url'
    3. import * as format from './format.js'
    4.  
    5. const isMain = process.argv[1] &&
    6. await realpath(url.fileURLToPath(import.meta.url)) ===
    7. await realpath(process.argv[1])
    8.  
    9. if (isMain) {
    10. const { default: pino } = await import('pino')
    11. const logger = pino()
    12. logger.info(format.upper('my-package started'))
    13. process.stdin.resume()
    14. }
    15.  
    16. export default (str) => {
    17. return format.upper(str).split('').reverse().join('')
    18. }
  • Posts: 2

    That did it. Good eye. Thank you guillermotti

  • Posts: 160
    edited November 2022

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

  • Posts: 2,295

    This has been corrected. Thank you!

This discussion has been closed.

Welcome!

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

Categories

Upcoming Training