Hi there,
I added webpacker using the instructions provided. I added this to my gemfile:
gem 'webpacker', github: 'rails/webpacker'
I'm using Rails 5.1.0rc1. I ran the commands:
./bin/rails webpacker:install
./bin/rails webpacker:install:react
I have made no other changes at all.
When running in development, it runs ok, but however when trying to compile the assets, I'm getting this (same error when using assets:precompile):
$ bundle exec rake webpacker:compile
Webpacker is installed 馃帀 馃嵃
Using /data/deployer/timeline/current/config/webpack/paths.yml file for setting up webpack paths
Compiling webpacker assets 馃帀
/data/deployer/timeline/current/config/webpack/shared.js:6
const { basename, join, resolve } = require('path')
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/data/deployer/timeline/current/config/webpack/production.js:7:22)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
rake aborted!
JSON::ParserError: 743: unexpected token at ''
/var/lib/gems/2.3.0/gems/json-2.0.3/lib/json/common.rb:156:in `parse'
/var/lib/gems/2.3.0/gems/json-2.0.3/lib/json/common.rb:156:in `parse'
/data/deployer/.bundler/ruby/2.3.0/webpacker-3c488f9f4022/lib/tasks/webpacker/compile.rake:11:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => webpacker:compile
(See full trace by running task with --trace)
The file has not been modified at all:
$ head -n 8 /data/deployer/timeline/current/config/webpack/shared.js
// Note: You must restart bin/webpack-watcher for changes to take effect
/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */
const webpack = require('webpack')
const { basename, join, resolve } = require('path')
const { sync } = require('glob')
const { readdirSync } = require('fs')
Any ideas?
I use ruby 2.4.0p0. json 2.0.3 gem. I use rails 5.1.0.rc1 and webpacker's master branch. Did webpacker:install and webpacker:install:react
It compiles successfully. Perhaps, you can try with a fresh project again to see if this persists?
This is my shared.js in case you wish to compare it.
// Note: You must restart bin/webpack-watcher for changes to take effect
/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */
const webpack = require('webpack')
const { basename, join, resolve } = require('path')
const { sync } = require('glob')
const { readdirSync } = require('fs')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const ManifestPlugin = require('webpack-manifest-plugin')
const extname = require('path-complete-extname')
const { env, paths, publicPath, loadersDir } = require('./configuration.js')
const extensionGlob = `*{${paths.extensions.join(',')}}*`
const packPaths = sync(join(paths.source, paths.entry, extensionGlob))
module.exports = {
entry: packPaths.reduce(
(map, entry) => {
const localMap = map
localMap[basename(entry, extname(entry))] = resolve(entry)
return localMap
}, {}
),
output: { filename: '[name].js', path: resolve(paths.output, paths.entry) },
module: {
rules: readdirSync(loadersDir).map(file => (
require(join(loadersDir, file))
))
},
plugins: [
new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
new ExtractTextPlugin(env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css'),
new ManifestPlugin({ fileName: 'manifest.json', publicPath, writeToFileEmit: true })
],
resolve: {
extensions: paths.extensions,
modules: [
resolve(paths.source),
resolve(paths.node_modules)
]
},
resolveLoader: {
modules: [paths.node_modules]
}
}
@xanview Are you getting that error locally or during deployment to production? Are node_modules installed correctly? Just to be sure please run ./bin/yarn install to re-install deps. Which node version are you using?
I can't reproduce this locally or in production (heroku)
This looks like an issue with ES6 const keyword. What Node version are you using on your production environment?
Upgrading from Node 5.12 to 6.10 solved the same issue for me.
Perhaps we document node.js version too in the readme to make sure this doesn't happen.
This is now documented in the readme. @xanview Feel free to close the issue.
I'm running nodejs 4.2.6~dfsg-1ubuntu4.1 - I'm guessing that's the issue? - It is the latest available on chris-lea/node.js PPA which I'm guessing is deprecated?
Yepp 馃憤 Please use node >= 6.4
@gauravtiwari What changes in node 6.4 are required for this? I've successfully used it on machines with node 6.2.0. Is the requirement really >= 6.0, or is there some issue with minor versions 6.0 - 6.3 that I've just been lucky to avoid running into?
@rmacklin Yepp for es6 the requirement really is >= 6.0, since both versions produces identical results. Node 6.4 was more stable though in the band (6.0...6.4) so went with that to ensure everything works.
I had this exact same problem when running rails s
For me the solution was to simply delete my local app and pull it again from my remote repository. No issue with Node version.
Most helpful comment
This looks like an issue with ES6
constkeyword. What Node version are you using on your production environment?