gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:31:44 25 4
gpt4 key购买 nike

我正在绞尽脑汁试图将应用程序部署到 Heroku。问题是我正在将 ES6 与 Babel 一起使用。

我找到了很多有关此问题的文章,但没有一篇可以帮助我解决问题

即使我在本地构建应用程序并尝试运行它,我仍然遇到另一个问题 - 构建文件夹中模块的路径保持与通过 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

结构

enter image description here

配置文件

web: npm run start

最佳答案

您的 server.js 文件似乎与 /environment 位于同一目录中。

但是您指定的从 server.js/environment 的路径前面只能有一个句点。将 ../environment 更改为 ./environment.

编辑

看来该项目只需要一个模块路径解析器,babel 提供了一个:babel-plugin-module-resolver

通过正确的配置将其添加到构建过程中应该允许您简单地使用放置到构建中的正确路径进行部署

关于javascript - 如果使用 Babel 和 ES6,如何将应用程序部署到 Heroku?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60606161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com