node.js - 尝试在我的 React 应用程序 digitalocean Droplet 上使用 pm2 时出现意外的 token 导入

标签 node.js reactjs npm digital-ocean pm2

我尝试在 DigitalOcean 上托管我的应用程序并使其与 pm2 一起运行,但遇到此错误:

/home/bcavenagh/social/src/index.js:1
(function (exports, require, module, __filename, __dirname) { import React from 'react';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)

使用 npm start 时我可以运行所有内容,但它不适用于 pm2。在 npm 中,我从 ~/social 文件夹运行,但对于 pm2,我 cd 到 ~/social/src 并运行:

pm2 start index.js

如何克服这个错误?

最佳答案

如果没有这样做,您必须编译/构建您的应用程序。

它将创建一个构建文件夹(假设您使用 create-react-app 的默认配置),然后您必须将 pm2 指向该文件夹中的适当文件(bundle.js 或您拥有的任何文件)。

PM2 现在还支持运行 npm start 命令。查看 this 问题。我通常做的是,编写一个 sh 文件,该文件更新代码,执行 npm install,然后最后运行 pm2 命令,用于所有部署,这与上一个链接中的 this answer 类似。

关于node.js - 尝试在我的 React 应用程序 digitalocean Droplet 上使用 pm2 时出现意外的 token 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54068520/

相关文章:

javascript - React Stytch 尚未加载

javascript - Meteor Npm.require() 在父目录中找不到文件

node.js - 使用来自 casperjs 的 Node 模块

node.js - 如何在我的服务器上的控制台中访问我的nodeJS应用程序?

javascript - Node.js for() 循环在每个循环中返回相同的值

node.js - 为什么安装 io.js 后无法再安全复制

reactjs - React js material ui textfield helper 文本位置

reactjs - React 样式组件条件三元运算符

javascript - NPM在nodejs中获取中止错误

javascript - 处理 JavaScript 中的异步行为?