📌  相关文章
📜  (node:5547) UnhandledPromiseRejectionWarning: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension \".json\" - Javascript (1)

📅  最后修改于: 2023-12-03 14:38:45.453000             🧑  作者: Mango

Node.js Unhandled Promise Rejection Warning: Unknown File Extension

As a Node.js developer, you might have encountered the UnhandledPromiseRejectionWarning. This warning message indicates that a rejected promise was not handled in your code.

One common cause of this warning is when you try to require a file with an unknown file extension.

For instance, the error message (node:5547) UnhandledPromiseRejectionWarning: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" usually occurs when you try to require a JSON file that has an unknown extension.

To fix this error, you need to specify the correct extension of the file you are requiring. In this case, change the file extension to .js or add the .json extension to the list of known extensions in your require.extensions object.

Here's an example of how to add the .json extension to the list of known extensions:

require.extensions['.json'] = function (module, filename) {
  // Your code to handle JSON files here
};

In conclusion, always ensure that you handle rejected promises properly and specify the correct file extension when requiring files to avoid the UnhandledPromiseRejectionWarning error.