简体   繁体   English

npm start throwing error status 1 require is not a function

[英]npm start throwing error status 1 require is not a function

I get the following error when running npm start.运行 npm start 时出现以下错误。

> app@0.1.0 start /Users/user/Desktop/react-tutorial
> react-scripts start

require(...) is not a function
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the 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:

Here is the actual error这是实际的错误

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
2 info using npm@6.4.1
3 info using node@v10.15.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle app@0.1.0~prestart: app@0.1.0
6 info lifecycle app@0.1.0~start: app@0.1.0
7 verbose lifecycle app@0.1.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle app@0.1.0~start: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/user/Desktop/react-tutorial/node_modules/.bin:/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/bin:/Users/user/Library/Android/sdk/tools:/Users/user/Library/Android/sdk/platform-tools:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin:/opt/local/share/java/android-sdk-macosx/platform-tools:/Users/user/.android-sdk-macosx/platform-tools/:/Users/user/.android-sdk-macosx/platform-tools/:/Users/user/Library/Android/sdk/platform-tools/
9 verbose lifecycle app@0.1.0~start: CWD: /Users/user/Desktop/react-tutorial
10 silly lifecycle app@0.1.0~start: Args: [ '-c', 'react-scripts start' ]
11 silly lifecycle app@0.1.0~start: Returned: code: 1  signal: null
12 info lifecycle app@0.1.0~start: Failed to exec start script
13 verbose stack Error: app@0.1.0 start: `react-scripts start`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:962:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
14 verbose pkgid app@0.1.0
15 verbose cwd /Users/user/Desktop/react-tutorial
16 verbose Darwin 16.7.0
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
18 verbose node v10.15.0
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error app@0.1.0 start: `react-scripts start`
22 error Exit status 1
23 error Failed at the 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 [ 1, true ]

This just started to happen when i restarted computer...why当我重新启动计算机时,这才开始发生......为什么

I can't find anywhere where I am calling require()... maybe a plugin I installed?我在任何地方都找不到我调用 require() 的地方……也许我安装了一个插件?

In my case, had created a setupProxy.js file at project level, later i removed the configurations in it but not the file.就我而言,在项目级别创建了一个 setupProxy.js 文件,后来我删除了其中的配置,但没有删除该文件。 Hence the mentioned error in question was thrown.因此,所提到的错误被抛出。 To resolve, try one of the following according to your need,要解决,请根据您的需要尝试以下方法之一,

  1. update the proper configuration in the setupProxy.js更新 setupProxy.js 中的正确配置
  2. delete the setupProxy.js if not needed如果不需要,请删除 setupProxy.js
  3. don't leave the setupProxy.js as empty file不要将 setupProxy.js 保留为空文件
  1. 删除nodes_modules
  2. npm install -g npm
  3. npm install & npm install -D
  4. npm run start

Thanks to @Srinivas's response in the comment of the question.感谢@Srinivas 在问题评论中的回复。

Delete the generated setupProxy.js file.删除生成的setupProxy.js文件。 If you want to use a proxy, instead add it into your package.json file.如果您想使用代理,请将其添加到您的package.json文件中。

  "proxy": "http://localhost:3000",

For a setupProxy.js file like so:对于像这样的setupProxy.js文件:

const proxy = require('http-proxy-middleware');

// tells the browser to direct any relative reference paths to the below 
// domain instead of our own
module.exports = function(app) {
  app.use(proxy('/api/*', { target: 'http://localhost:3000' }));*/
};

When you are setting up a Proxy to make a request to a different server当您设置代理以向不同的服务器发出请求时

First Step is to install the dependency第一步是安装依赖

npm install --save http-proxy-middleware

Next create setupProxy.js file接下来创建 setupProxy.js 文件

setupProxy.js设置代理.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
       target: 'http://localhost:3001',
       changeOrigin: true
    })
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM