Skip to main content

省略后缀名

省略后缀名

webpack.config.jsmodule.exports 下加入以下代码:

resolve:{
extensions:['.js','.jsx','.json']
}

需要注意的是,如果省略不写resolve的话,会默认补全js和json后缀名,需要注意这些后缀名的顺序,代表着系统进行后缀名补全的尝试顺序。 最后配置完毕之后,一定要重启项目,否则可能会因为缓存而报错,像这样:

Uncaught Error: Cannot find module './components/Hello'
client:150 [WDS] Errors while compiling. Reload prevented.
errors @ client:150
client:159 ./src/index.js
Module not found: Error: Can't resolve './components/Hello' in 'C:\Users\dell\Desktop\webpack\src'