nodeJS require.paths resolve problem

后端 未结 3 1913
长发绾君心
长发绾君心 2021-02-14 20:03

I am trying to require a file relatively and mysteriously the following is happening

This works well which points to /Users/marcos/Desktop/Taper/lib/utils.js

3条回答
  •  太阳男子
    2021-02-14 21:01

    UPDATED:

    From the documentation:

    A module prefixed with '/' is an absolute path to the file. For example, require('/home/marco/foo.js') will load the file at /home/marco/foo.js.

    A module prefixed with './' is relative to the file calling require(). That is, circle.js must be in the same directory as foo.js for require('./circle') to find it.

    Without a leading '/' or './' to indicate a file, the module is either a "core module" or is loaded from a node_modules folder.

    If the given path does not exist, require() will throw an Error with its code property set to 'MODULE_NOT_FOUND'.


    Here’s the original answer, which refers to require.paths (which is no longer supported):

    From the documentation:

    In node, require.paths is an array of strings that represent paths to be searched for modules when they are not prefixed with '/', './', or '../'.

    (emphasis mine)

提交回复
热议问题