无法使用 d3 部署 Rails 6 应用程序。在本地运行但 Heroku 构建中断:ModuleNotFoundError: Module not found: Error: Can't resol
Posted
技术标签:
【中文标题】无法使用 d3 部署 Rails 6 应用程序。在本地运行但 Heroku 构建中断:ModuleNotFoundError: Module not found: Error: Can\'t resolve \'d3\'【英文标题】:Unable to deploy a Rails 6 app using d3. Runs locally but Heroku build breaks: ModuleNotFoundError: Module not found: Error: Can't resolve 'd3'无法使用 d3 部署 Rails 6 应用程序。在本地运行但 Heroku 构建中断:ModuleNotFoundError: Module not found: Error: Can't resolve 'd3' 【发布时间】:2020-09-15 05:06:12 【问题描述】:我在模块中这样导入 d3:
import * as d3 from "d3"
该应用程序在 localhost 上按预期工作,并生成所需的图表。但是当我推送到 heroku 时,预编译失败。
我没有接触任何 webpack 或 webpacker 配置,一切都是 Rails 提供的。
除了通过在 import 语句中提供到 d3 的直接路径来解决这种情况外,我没有发现太多关于这种情况的提及。我试过了,但错误没有改变。
关于如何解决这个问题并确保成功部署到 Heroku 的任何想法?
谢谢!
package.json
"name": "the_app",
"private": true,
"dependencies":
"@rails/actioncable": "^6.0.0",
"@rails/activestorage": "^6.0.0",
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "4.2.2",
"turbolinks": "^5.2.0"
,
"version": "0.1.0",
"devDependencies":
"d3": "^5.16.0",
"webpack-dev-server": "^3.10.3"
错误
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: yarn install v1.16.0
remote: [1/4] Resolving packages...
remote: [2/4] Fetching packages...
remote: info fsevents@1.2.12: The platform "linux" is incompatible with this module.
remote: info "fsevents@1.2.12" is an optional dependency and failed compatibility check. Excluding it from installation.
remote: [3/4] Linking dependencies...
remote: warning " > webpack-dev-server@3.10.3" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0".
remote: warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has unmet peer dependency "webpack@^4.0.0".
remote: [4/4] Building fresh packages...
remote: Done in 23.83s.
remote: yarn install v1.16.0
remote: [1/4] Resolving packages...
remote: [2/4] Fetching packages...
remote: info fsevents@1.2.12: The platform "linux" is incompatible with this module.
remote: info "fsevents@1.2.12" is an optional dependency and failed compatibility check. Excluding it from installation.
remote: [3/4] Linking dependencies...
remote: warning " > webpack-dev-server@3.10.3" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0".
remote: warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has unmet peer dependency "webpack@^4.0.0".
remote: [4/4] Building fresh packages...
remote: Done in 4.98s.
remote: I, [2020-05-28T00:58:16.236509 #435] INFO -- : Writing /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/public/assets/application-e9ef9b65582d84e0bcb1825908e96a08d055278ce584c687748a7dbd3a739747.css
remote: I, [2020-05-28T00:58:16.236817 #435] INFO -- : Writing /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/public/assets/application-e9ef9b65582d84e0bcb1825908e96a08d055278ce584c687748a7dbd3a739747.css.gz
remote: I, [2020-05-28T00:58:16.237059 #435] INFO -- : Writing /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/public/assets/main-e9ef9b65582d84e0bcb1825908e96a08d055278ce584c687748a7dbd3a739747.css
remote: I, [2020-05-28T00:58:16.237229 #435] INFO -- : Writing /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/public/assets/main-e9ef9b65582d84e0bcb1825908e96a08d055278ce584c687748a7dbd3a739747.css.gz
remote: Compiling...
remote: Compilation failed:
remote: ModuleNotFoundError: Module not found: Error: Can't resolve 'd3' in '/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/modules'
remote: at factory.create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/lib/Compilation.js:925:10)
remote: at factory (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/lib/NormalModuleFactory.js:401:22)
remote: at resolver (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/lib/NormalModuleFactory.js:130:21)
remote: at asyncLib.parallel (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/lib/NormalModuleFactory.js:224:22)
remote: at /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/neo-async/async.js:2830:7
remote: at /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/neo-async/async.js:6877:13
remote: at normalResolver.resolve (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/lib/NormalModuleFactory.js:214:25)
remote: at doResolve (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:213:14)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
remote: at resolver.doResolve (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:44:7)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
remote: at resolver.doResolve (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:67:43)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn41 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:28:1)
remote: at resolver.doResolve (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/ModuleKindPlugin.js:30:40)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn1 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:16:1)
remote: at args (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/forEachBail.js:30:14)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
remote: at resolver.doResolve (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:44:7)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
remote: at resolver.doResolve (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:67:43)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn42 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:16:1)
remote: at hook.callAsync (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/webpack/node_modules/enhanced-resolve/lib/Resolver.js:285:5)
remote: at _fn0 (eval at create (/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
remote: resolve 'd3' in '/tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/modules'
remote: Parsed request is a module
remote: using description file: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/package.json (relative path: ./app/javascript/modules)
remote: Field 'browser' doesn't contain a valid alias configuration
remote: resolve as module
remote: looking for modules in /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript
remote: using description file: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/package.json (relative path: ./app/javascript)
remote: Field 'browser' doesn't contain a valid alias configuration
remote: using description file: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/package.json (relative path: ./app/javascript/d3)
remote: no extension
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3 doesn't exist
remote: .mjs
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.mjs doesn't exist
remote: .js
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.js doesn't exist
remote: .sass
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.sass doesn't exist
remote: .scss
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.scss doesn't exist
remote: .css
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.css doesn't exist
remote: .module.sass
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.module.sass doesn't exist
remote: .module.scss
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.module.scss doesn't exist
remote: .module.css
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.module.css doesn't exist
remote: .png
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.png doesn't exist
remote: .svg
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.svg doesn't exist
remote: .gif
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.gif doesn't exist
remote: .jpeg
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.jpeg doesn't exist
remote: .jpg
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3.jpg doesn't exist
remote: as directory
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/d3 doesn't exist
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/modules/node_modules doesn't exist or is not a directory
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/javascript/node_modules doesn't exist or is not a directory
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/app/node_modules doesn't exist or is not a directory
remote: /tmp/node_modules doesn't exist or is not a directory
remote: /node_modules doesn't exist or is not a directory
remote: looking for modules in /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules
remote: using description file: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/package.json (relative path: ./node_modules)
remote: Field 'browser' doesn't contain a valid alias configuration
remote: using description file: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/package.json (relative path: ./node_modules/d3)
remote: no extension
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3 doesn't exist
remote: .mjs
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.mjs doesn't exist
remote: .js
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.js doesn't exist
remote: .sass
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.sass doesn't exist
remote: .scss
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.scss doesn't exist
remote: .css
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.css doesn't exist
remote: .module.sass
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.module.sass doesn't exist
remote: .module.scss
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.module.scss doesn't exist
remote: .module.css
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.module.css doesn't exist
remote: .png
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.png doesn't exist
remote: .svg
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.svg doesn't exist
remote: .gif
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.gif doesn't exist
remote: .jpeg
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.jpeg doesn't exist
remote: .jpg
remote: Field 'browser' doesn't contain a valid alias configuration
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3.jpg doesn't exist
remote: as directory
remote: /tmp/build_ee117d42c87cfaf2c16b46c6bf931917/node_modules/d3 doesn't exist
remote:
remote:
remote: !
remote: ! Precompiling assets failed.
remote: !
remote: ! Push rejected, failed to compile Ruby app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to the-app
【问题讨论】:
您是否尝试将resolved_paths: ['app/javascript/packs']
添加到您的webpacker.yml
文件中?
应用程序已经在 dev 上运行,并且包正在 dev 上正确构建,在 prod 上失败。我找到了罪魁祸首 - 已将 d3 安装为仅开发依赖项。
【参考方案1】:
问题是我已将 d3 安装为仅开发依赖项,如 package.json 文件中所示。
yarn add d3
解决了这个问题。
【讨论】:
以上是关于无法使用 d3 部署 Rails 6 应用程序。在本地运行但 Heroku 构建中断:ModuleNotFoundError: Module not found: Error: Can't resol的主要内容,如果未能解决你的问题,请参考以下文章
升级弹性 beantalk 堆栈时,Rails 应用程序无法部署
Heroku 推送错误:“无法检测到 rake 任务”(Rails 6.1)
在heroku上使用vue应用程序部署rails 6时出现node-gyp错误