如果您使用Babel和ES6,如何将应用程序部署到Heroku?

iswrvxsc  于 2022-12-08  发布在  Babel
关注(0)|答案(2)|浏览(124)

我正在绞尽脑汁地试图将应用程序部署到Heroku上。问题是我正在使用带有Babel的X1 M0 N1 X。
我找到了很多关于这方面的文章,但没有一篇能帮助我解决这个问题
即使我在本地构建应用程序并尝试运行它,我也会遇到另一个问题-build文件夹中指向模块的路径与Babel-build编译之前保持相同这是一个普通的行为吗,因为我以前从未遇到过这种情况
它应该启动GraphQL API -“heroku_path/graphql”
我会很感激你的帮助...
package.json

{
  "name": "cocoon-api",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "main": "src/server.js",
  "scripts": {
    "dev": "babel-watch src/server.js",
    "clean": "rm -rf build && mkdir build",
    "build-babel": "babel -d ./build ./src -s",
    "build": "npm run clean && npm run build-babel",
    "start": "npm run build && node ./build/server.js",
    "heroku-postbuild": "npm install"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "apollo-server-express": "^2.9.14",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "graphql": "^14.5.8",
    "graphql-depth-limit": "^1.1.0",
    "graphql-iso-date": "^3.6.1",
    "graphql-middleware": "^4.0.2",
    "graphql-shield": "^7.0.6",
    "graphql-tag": "^2.10.1",
    "graphql-toolkit": "^0.7.5",
    "graphql-tools": "^4.0.6",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.15",
    "merge-graphql-schemas": "^1.7.4",
    "mongoose": "^5.8.3",
    "winston": "^3.2.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.7.7",
    "@babel/core": "^7.7.7",
    "@babel/node": "^7.7.7",
    "@babel/preset-env": "^7.7.7",
    "babel-plugin-import-graphql": "^2.7.0",
    "babel-watch": "^7.0.0",
    "dotenv": "^8.2.0",
    "nodemon": "^2.0.2"
  }
}

部署时出错

$ git push heroku master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 344 bytes | 344.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 12.x...
remote:        Downloading and installing node 12.16.1...
remote:        Using default npm version: 6.13.4
remote:
remote: -----> Restoring cache
remote:        - node_modules
remote:
remote: -----> Installing dependencies
remote:        Installing node modules (package.json + package-lock)
remote:        audited 7150 packages in 8.044s
remote:
remote:        13 packages are looking for funding
remote:          run `npm fund` for details
remote:
remote:        found 143 low severity vulnerabilities
remote:          run `npm audit fix` to fix them, or `npm audit` for details
remote:
remote: -----> Build
remote:        Detected both "build" and "heroku-postbuild" scripts
remote:        Running heroku-postbuild
remote:
remote:        > cocoon-api@1.0.0 heroku-postbuild /tmp/build_900f276e6b5aac117b7994386823a0a9
remote:        > npm install
remote:
remote:        audited 7150 packages in 8.008s
remote:
remote:        13 packages are looking for funding
remote:          run `npm fund` for details
remote:
remote:        found 143 low severity vulnerabilities
remote:          run `npm audit fix` to fix them, or `npm audit` for details
remote:
remote: -----> Caching build
remote:        - node_modules
remote:
remote: -----> Pruning devDependencies
remote:        Skipping because NPM_CONFIG_PRODUCTION is 'false'
remote:
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 31.5M
remote: -----> Launching...
remote:        Released v11
remote:        https://aqueous-springs-82481.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/aqueous-springs-82481.git
   5cd7f10..4abee83  master -> master

路径错误

Successfully compiled 29 files with Babel.
internal/modules/cjs/loader.js:613
    throw err;
    ^

Error: Cannot find module '../environment'
Require stack:
- C:\Users\Валерий\Documents\Projects\cocoon-api\build\server.js

构造物

过程文件

web: npm run start
suzh9iv8

suzh9iv81#

server.js文件似乎与/environment位于同一目录中。
但是,您指定的从server.js/environment的路径前面应该只有一个句点。请将../environment更改为./environment
编辑
这个项目似乎只需要一个模块路径解析器,Babel提供了一个:babel-plugin-module-resolver
通过将正确的配置添加到构建过程中,您可以简单地将正确的路径放置到构建中进行部署

nkkqxpd9

nkkqxpd92#

别紧张,
1.为模块路径解析器添加此插件:babel-plugin-module-resolver
1.在根目录中创建一个名为babel.config.js的文件,在开发中我使用src文件夹,在生产中我在dist文件夹中构建我的应用程序(在您的情况下,您使用build文件夹)示例:

module.exports = {
   presets: [
     ['@babel/preset-env', { targets: { node: 'current' } }],
     '@babel/preset-typescript'
   ],
   plugins: [
     ['module-resolver', {
       alias: {
         "@modules": "./src/modules",
         "@config": "./src/config",
         "@shared": "./src/shared"
       }
     }],
     "babel-plugin-transform-typescript-metadata",
     ["@babel/plugin-proposal-decorators", { "legacy": true }],
     ["@babel/plugin-proposal-class-properties", { "loose": true }],
   ],
 }

1.将构建脚本添加到package.json(使用YARN或NPM):

"scripts": {
   "build": "yarn babel src --extensions \".js,.ts\" --out-dir dist --copy-files && npx typeorm migration:run",
   "start": "node dist/shared/infra/http/server.js",
 }

在我的例子中,我不需要一个脚本来清理/删除旧的dist文件夹(或者在你的例子中是build)。
如果成功了告诉我。

相关问题