Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

Javascript used in rails with webpacker doesn't give any console.log output

I have this code in application.js

require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")

import start from './games/chatcher'

console.log(1)

export {
    start
}

but i don't get any output in console

Why?

Edit: my webpacker related lines in package.json

{
    "@rails/webpacker": "^6.0.0-beta.2",
    "turbolinks": "^5.2.0",
    "webpack": "^5.11.0",
    "webpacker-cli": "^2.2.1"
  },
  "version": "0.1.0",
  "devDependencies": {
    "@webpack-cli/serve": "^1.2.1",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.2"
  },
 }

and my Gemfile

gem 'webpacker', '~> 6.x'

I assume that it could be because I updated webpacker and probably some versions don't work correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put it in a turbolinks:load listener:

document.addEventListener('turbolinks:load', () => {
  console.log(1)
}

You can also check if webpack is compiling correctly by running bin/webpack in your terminal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...