Node.js在npm启动时找不到模块错误

时间:2020-07-18 10:06:39

标签: node.js reactjs npm npm-start

当我运行npm start命令来启动我的第一个React项目的服务器时,出现如下错误。我尝试卸载并重新安装node.js,但是没有用。我该如何解决这个问题?我在这里包括我的package.json和目录结构。 这是显示的错误:

    > reactproject1@1.0.0 start C:\Users\LENOVO\reactproject1
    > node index.js
    
    internal/modules/cjs/loader.js:969
      throw err;
      ^
    
    Error: Cannot find module 'C:\Users\LENOVO\reactproject1\index.js'
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
        at Function.Module._load (internal/modules/cjs/loader.js:842:27)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
        at internal/main/run_main_module.js:17:47 {
      code: 'MODULE_NOT_FOUND',
      requireStack: []
    }
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! reactproject1@1.0.0 start: `node index.js`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the reactproject1@1.0.0 start script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\LENOVO\AppData\Roaming\npm-cache\_logs\2020-07-18T09_12_04_972Z-debug.log
    
    
    
This is my _package.json_ file of my react project. I have edited it by adding `"start":"node index.js"`. 

```javascript
{
  "name": "reactproject1",
  "version": "1.0.0",
  "description": "restropart",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },

  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  }
}
```

    My Directory tree is as follows
    -ReactProject
      -node_modules
      -public
      -src
        ->index.js
      -package.json
      -package-lock.json
My index.js file resides inside the src folder.The node_modules,public,src,package.json,package-lock.json are on the same line

1 个答案:

答案 0 :(得分:0)

启动脚本引用了index.js,但是您没有这样的文件-它位于src/index.js下。您可以将其移至项目根目录,也可以相应地更新start脚本:

"scripts": {
    "start": "node src/index.js"
},