Welcome to the Linux Foundation Forum!

Lab 7.1 ERR_UNSUPPORTED_ESM_URL_SCHEME

When execting npm test in Lab 7.1 on WIndows 11 with Node v16.14.0 installed I get the following error:

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'f:'
    at new NodeError (node:internal/errors:371:5)
    at throwIfUnsupportedURLProtocol (node:internal/modules/esm/resolve:1033:11)
    at defaultResolve (node:internal/modules/esm/resolve:1103:3)
    at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
    at ESMLoader.import (node:internal/modules/esm/loader:332:22)
    at importModuleDynamically (node:internal/modules/esm/translators:106:35)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
    at file:///F:/_jsnad/labs/ch-7/labs-1/test.mjs:4:39

Comments

  • cscharr
    cscharr Posts: 4

    I fixed the test by slightly changing the module loading according to your own tutorials in the chapter ;)
    Adding pathToFileURL

    import { pathToFileURL } from 'url'
    import { createRequire } from 'module'
    import assert from 'assert'
    const { resolve } = createRequire(import.meta.url)
    const { default: add } = await import(pathToFileURL(resolve('.')).toString())
    
    assert(typeof add === 'function', 'function exported')
    
    assert(add(15, 7) + add(11, 9) === 42, 'correct function exported')
    
    console.log('passed!')
    
  • xdxmxc
    xdxmxc Posts: 157

    thanks @cscharr nicely done! code is being updated

Categories

Upcoming Training