React项目执行npm run build报错:index.js源文件路径错误
Hey there, let's break down this build error you're hitting. The key line in your error log tells us exactly what's going wrong:
Module not found: Error: Can't resolve '/Users/david/git/demoapp/demo/src/index.js' in '/Users/david/git/demoapp'
Webpack (which nwb uses under the hood) can't locate your demo app's entry file. Here are the most common fixes to try:
Double-check the file path and existence
Head over to/Users/david/git/demoapp/demo/src/in your file system and make sure:- The
srcfolder actually exists inside thedemodirectory - There's an
index.jsfile in that folder (watch out for typos or capitalization issues—likeIndex.jsinstead ofindex.js, since some environments are case-sensitive)
- The
Verify your nwb configuration
If you have a customnwb.config.jsfile in your project root, open it and check thedemoentry configuration. It should point to the correct path of your index.js. For reference, the default nwb setup expects:module.exports = { demo: { entry: './demo/src/index.js' } }If this path was modified incorrectly, that's definitely causing the issue.
Clean build caches and retry
Sometimes stale cache files can cause weird path resolution issues. Run these commands to clear things up:nwb clean npm run buildIf that doesn't work, you can also delete the
node_modules/.cachefolder manually before rebuilding.Confirm your project structure matches nwb's expectations
Nwb has standard project structures for React apps. A typical setup for a demo should look like this:demoapp/ ├── demo/ │ └── src/ │ └── index.js ├── node_modules/ └── package.jsonIf your files are arranged differently, you'll need to either adjust your directory structure to match this or update the entry path in your nwb config.
Give these steps a try—most of the time, this error boils down to a missing file or a typo in the path.
内容的提问来源于stack exchange,提问作者oldDave

