2017-09-29 41 views
0

我已經制作了一個使用create-react-app的web應用程序,它工作正常,我通常使用npm start運行它,但現在我必須部署它並使其與nodejs forever模塊配合使用。nodejs啓動react-app客戶端

如果我這樣做node src/index.js它會給一個語法錯誤並解決這個問題我以前通天transpile es6es5,但再次導入CSS文件時,我得到了以下語法錯誤:

/client/src/stylesheets/menu.css:1 
(function (exports, require, module, __filename, __dirname) { header { 
                    ^

SyntaxError: Unexpected token { 

這是我package.json文件:

{ 
    "dependencies": { 
    "async": "^2.5.0", 
    "axios": "^0.16.2", 
    "classnames": "^2.2.5", 
    "date-diff": "^0.1.3", 
    "jwt-decode": "^2.2.0", 
    "lodash": "^4.17.4", 
    "react": "^15.6.1", 
    "react-bootstrap": "^0.31.2", 
    "react-bootstrap-table": "^4.0.2", 
    "react-dom": "^15.6.1", 
    "react-redux": "^5.0.6", 
    "react-router": "^4.2.0", 
    "react-router-dom": "^4.2.2", 
    "react-scripts": "1.0.12", 
    "react-select": "^1.0.0-rc.5", 
    "redux": "^3.7.2", 
    "redux-thunk": "^2.2.0", 
    "shortid": "^2.2.8", 
    "validator": "^8.1.0" 
    }, 
    "scripts": { 
    "start": "react-scripts-with-stylus start src/stylesheets/menu.styl", 
    "build": "react-scripts-with-stylus build src/stylesheets/menu.styl", 
    "test": "react-scripts test --env=jsdom", 
    "eject": "react-scripts eject" 
    }, 
    "proxy": "http://localhost:8081", 
    "devDependencies": { 
    "babel": "^6.23.0", 
    "babel-cli": "^6.24.1", 
    "babel-core": "^6.25.0", 
    "babel-polyfill": "^6.23.0", 
    "babel-preset-es2015": "^6.24.1", 
    "babel-preset-stage-0": "^6.24.1", 
    "create-react-app-stylus": "^1.1.1", 
    "webpack": "^3.5.5" 
    } 
} 
+0

如果您需要部署它,您應該進行構建,而不是直接運行您的應用程序。 – Shota

+0

我如何構建?這是我的第一個反應應用程序,任何信息都會幫助我很多......我試圖將它部署到數字海洋 – Valip

+0

run'npm run build',它會將生成文件生成到一個新文件夾中。 – Shota

回答

1

答案總結這個問題:

  • 如果您需要部署它,您應該進行構建,而不是直接運行應用程序。
  • 運行npm run build它會將生成文件生成到一個新文件夾中。
  • 你不應該試圖用npm來運行應用程序。您應該將build文件夾中的所有文件複製到服務器的根目錄下,當用戶打開該頁面時,它將顯示index.html文件。
  • 如果您的代碼已更改並已準備好進行生產,您可以重新構建它,也可以使用某個CI工具自動執行此過程。
  • 你應該只有你的Git倉庫上的React客戶端文件,生成文件應該在你的服務器上的生成過程中生成。