简体   繁体   中英

Node.js requiring a file

I have a file inside the app folder.

What is the correct way to require it in Node?

I am using const to_be_required = require('./to_be_required.js');

But this give me this error:

module.js:549
    throw err;
    ^

Error: Cannot find module 'got'

That is the correct way to require files in Node. You may need to run npm install or there's a typo in another file.

Your syntax is correct, however the module you are trying to point to either does not exist or there is some other issue with where the module you are looking for is located. You need to understand what "./" means when requiring modules.

./ is same directory

../ is the previous directory (kind of like cd ..)

Node modules are different. You don't need to explicitly state where a particular module you want to require is. In this case, ./ is enough, but make sure you used npm install in order to install the node module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM