Welcome to the Linux Foundation Forum!

Lesson 7: Resolving a module path, wrong example?

Hello, I was doing some exercises by my self and I found a fault in the lesson 7, last part before exercises.

In the example:
console.log('require('.')', '\t\t', ' =>', require.resolve('.'))
Should log the directory path but I got an error that tell me Error: Cannot find module '.'

Can you confirm me if you test that code logs you the directory?
I think you gonna get the same error than me but only for confirm

I suggest to change that line of code for console.log(path.resolve(__dirname, '.')) that this YES logs me the directory.

Node version: 14.15.3

Comments

  • @xwarzdev this lesson isn't about directory resolution, but understanding the package dependencies.

    require.resolve is looking up a module, the module of the current directory. This is failing because the code isn't being run in a module. Before this part of the chapter we've already created a my-package folder and an index.js file. If this code isn't run from the my-package folder it will fail.

  • @davidmarkclements I do have an error message for 7.1 and 7.2 regarding the npm test run as shown in attached pdf file. When index.js is executed with node it outputs 42.

  • @davidmarkclements also for the my-package exercise it was not possible to run the command without getting an error message back as attached in the pdf file:

    node -p "require('./index.js')('hello')"
    TypeError: require(...) is not a function

    Do you have any suggestions or ideas, please?

Categories

Upcoming Training