【问题标题】:How do I get npm start to work on Ubuntu?如何让 npm start 在 Ubuntu 上工作?
【发布时间】:2021-03-24 09:57:16
【问题描述】:

我知道这个问题已经发生在很多人身上很多次了,但我昨天花了几个小时试图完成这项工作,但我终生无法解决。

我正在尝试学习 React,当我尝试使用“npm start”命令时,它是这样的:

$ npm start

> react-app@0.1.0 start /react-app
> /react-app/node_modules/react-scripts/scripts/start.js

    sh: 1: /react-app/node_modules/react-scripts/scripts/start.js: Permission denied
    npm ERR! code ELIFECYCLE
    npm ERR! errno 126
    npm ERR! react-app@0.1.0 start: `/react-app/node_modules/react-scripts/scripts/start.js`
    npm ERR! Exit status 126
    npm ERR! 
    npm ERR! Failed at the react-app@0.1.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!     /home/sven/.npm/_logs/2020-12-13T06_45_28_430Z-debug.log

我尝试过的事情:

  1. 为“启动”脚本添加执行权限
  2. 删除所有节点模块,重新安装节点,重新创建项目文件
  3. 编辑 package.json 以包含启动脚本

在我正在使用的教程中,这个人只需输入npm start,它就可以正常工作。如果有的话,有人能解释一下npm start 在这种情况下做了什么吗?我现在真的不知道该怎么办。

这就是我的 package.json 的样子:

{
  "name": "react-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "/home/sven/react-app/node_modules/react-scripts/scripts/start.js"
  },
  "dependencies": {
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1"
  }
}

这是日志文件:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'start' ]
2 info using npm@6.14.4
3 info using node@v10.19.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle react-app@0.1.0~prestart: react-app@0.1.0
6 info lifecycle react-app@0.1.0~start: react-app@0.1.0
7 verbose lifecycle react-app@0.1.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle react-app@0.1.0~start: PATH: /usr/share/npm/node_modules/npm-lifecycle/node-gyp-bin:/react-app/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
9 verbose lifecycle react-app@0.1.0~start: CWD: /react-app
10 silly lifecycle react-app@0.1.0~start: Args: [ '-c',
10 silly lifecycle   '/react-app/node_modules/react-scripts/scripts/start.js' ]
11 silly lifecycle react-app@0.1.0~start: Returned: code: 126  signal: null
12 info lifecycle react-app@0.1.0~start: Failed to exec start script
13 verbose stack Error: react-app@0.1.0 start: `/react-app/node_modules/react-scripts/scripts/start.js`
13 verbose stack Exit status 126
13 verbose stack     at EventEmitter.<anonymous> (/usr/share/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:198:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/share/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:198:13)
13 verbose stack     at maybeClose (internal/child_process.js:982:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid react-app@0.1.0
15 verbose cwd /react-app
16 verbose Linux 5.4.0-58-generic
17 verbose argv "/usr/bin/node" "/usr/bin/npm" "start"
18 verbose node v10.19.0
19 verbose npm  v6.14.4
20 error code ELIFECYCLE
21 error errno 126
22 error react-app@0.1.0 start: `/react-app/node_modules/react-scripts/scripts/start.js`
22 error Exit status 126
23 error Failed at the react-app@0.1.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 126, true ]

【问题讨论】:

  • 你可以使用sudo来解决这个问题,但一般不推荐用于npm命令。
  • 也许chmod u+x /home/sven/react-app/node_modules/react-scripts/scripts/start.js ?
  • 我试过 chmod u+x,现在我得到一个不同的错误,它说 syscall spawn, errorno ENOENT,而不是退出状态 126

标签: javascript node.js reactjs ubuntu npm


【解决方案1】:

我也有这个问题。这是因为 REACT PROJECT 根文件夹的权限问题。当“npm start”或“npm run build”时。 react 应用程序将被构建,它需要在 react 项目目录中创建名为“build”的目录。请尝试

sudo chmod 777 {/path/dir_name}

它对我有用。 最后确保全局更新“npm”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 2014-04-17
    • 2017-07-15
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多