Require(‘./main.css’) in webpack; The Path Problem in

  node.js, question

Just touching the webpack, in a demo, the document structure is as follows:

clipboard.png

Css is introduced into index.js, i.e. require(‘./main.css’); But when I remove …/,I report the mistake. According to my idea, require(‘main.css’); Don’t you also find main.css based on index.js?
I hope you can help and guide me.

This is a commonJS-style dependency statement used by Noderequire()The characteristics of the decision.require(X)Statement, follow the following principles:

  • If x is equal to/./Or../At the beginning, it is considered as a relative path, and it is found according to the position of the title owner relative to the current file.

  • If it is not the beginning of the above, it is considered as a built-in module (e.g.require('path')), or atnode_modulesNpm package in inside (e.g.require('jquery'))。

Implemented by webpackrequire()This query principle is also followed. The topic owner can read Mr. Ruan’sThis article