-
Notifications
You must be signed in to change notification settings - Fork 27.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8.0.0 - babel not compiling properly #6273
Comments
Please provide a reproducible repository. |
@dunika I am having the same issue as you, out of curiosity do you have multiple babel configs? I do and it looks like babel is reading my .babelrc and .babelrc. |
@timneutkens here is a repo that reproduces the issue with https://github.com/kyle-mccarthy/nextjs8-babel-issue If you run I dug into it a little bit and think this commit is the problem. Modifying the primary .babelrc file and setting useESModules to false allows for the compiled server code to run correctly. For example:
|
I think have the same problem. I managed to reproduce it by modifying one of the existing examples. Using Next v7 this code compiles fine. Edit: |
Don't know if is related to this, but when trying to build, i get this:
|
I can confirm the same issue introduced by Thanks a lot, @kyle-mccarthy! I've spent hours debugging this on a complex project (custom server, different |
Here's another reproduction repo to help debugging the issue: github.com/icflorescu/next8-babel-issue. |
Re: #6273 (comment) Sounds like it's not really a Next.js issue then, you're trying to transpile the custom server, which is really not needed in nearly all use cases. The correct way to configure it (though again I really wouldn't recommend transpiling the custom server, really makes no sense to me) Adding a big warning here as I've seen this go wrong many times, sorry for the caps etc 😉: DO NOT PUT THIS BABELRC IN YOUR NEXT.JS ROOT DIRECTORY. YOU WILL OPT OUT OF TREE SHAKING ESMODULES. ONLY ADD IT TO THE SERVER DIRECTORY
|
@timneutkens I have to compile the server because I use the babel plugin module resolver. While I guess it isn't directly related to next or at least isn't a bug in next, it is probably worth mentioning in the docs. For anyone that does need to transpile their server side code, I ended up with a better fix by using the babel env options to define separate server and client envs. This allows for you to leave the useESModules option enabled. babel.config.js
Then in my package.json I have two different build steps:
|
Why do you need this? For zeit.co we have around 20 different routes and have everything contained without any compilation of the server. This indicates you're doing something non-standard / it can probably be improved a lot 🤔 |
@timneutkens the module resolver lets me reference files absolutely rather than having to specify a relative file path. I use typescript and in development I can define the paths/aliases in the tsconfig file and use the tsconfig-paths module; however, when you transpile the code from typescript to javascript, typescript doesn't replace those aliases with the relative file path. That is where the module-resolver comes in. Once the code is transpiled via tsc, I run it through babel to rewrite the alias with the relative path. Obviously the process is less than ideal, and it would be nice if typescript just replaced the paths when it transpiled the code, but since it doesn't I have to use babel for that. |
@timneutkens I too was running into an issue compiling after upgrading to Next 8 that was solved by modifying my babel preset-env to include Like @kyle-mccarthy I'm also using
As you can see, everything is in
in |
We’re experiencing the same issue as @Emiliano-Bucci describes it:
This goes away by turning on "commonjs" for both server and client (tried to do it only for the server but it did not work) but we’re not satisfied with shipping that to production. |
@timneutkens I think that transpiling on the server side is not as rare a use case as you might think. Anyone that has custom routing needs beyond the simple, out-of-the-box routing Next.js provides and uses TypeScript is in this boat. For me, this particular issue is holding me back from updating to 8.0 right now. I'm not super thrilled with idea of overriding the babel modules transform or performing a custom transpiling step, as all of this worked just fine in 7.0. |
@timneutkens I completely agree with @spencewood. Transpiling the server is definitely not a rare use case. There are, for instance, people using it just for the sake of being able to Hence, not being able to transpile the server in As a side note regarding tree-shaking in Next.js, it kind of works but it's far from perfect. |
I didn't say it's not possible, it's definitely possible to transpile the server, your babel config that compiled the server was wrong in the first place as it didn't account for transforming
I still strongly believe that this is the wrong tradeoff to make just to use different syntax to do the same thing, especially because the custom server should generally be just routing. Node.js supports most of es2016+ by now (even async/await natively) Just to be clear, I'm just trying to warn for a pitfall in your applications here. I'm not trying to be mean or tell you what to do 🙏 Everything will still work if you transpile the server, as Next.js is completely unaware that it's even running in a custom server environment. |
thank you very much, save my time |
Is there an official workaround for this issue, This also has prevented me from upgrading to Next.js ^8.x |
As said you probably have a wrong configuration: |
upgrade failed, down to next.js 7 |
I am now using, next@latest with a custom server. |
We also ran into this issue when upgrading from Next.js 7 to Next.js 8, likely due to our incorrect configuration. Here's the initial build error after upgrading: /Users/mherold/git/next-test/node_modules/@babel/runtime-corejs2/helpers/esm/objectSpread.js:1
(function (exports, require, module, __filename, __dirname) { import _Object$getOwnPropertyDescriptor from "../../core-js/object/get-own-property-descriptor";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Module._compile (/Users/mherold/git/next-test/node_modules/pirates/lib/index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.newLoader [as .js] (/Users/mherold/git/next-test/node_modules/pirates/lib/index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3) And here's the compile error we're seeing after implementing Failed to compile.
../node_modules/next/dist/client/next-dev.js 34:6
Module parse failed: Unexpected token (34:6)
You may need an appropriate loader to handle this file type.
|
|
> import('./noop');
| var _window = window,
| assetPrefix = _window.__NEXT_DATA__.assetPrefix; Finally, here are various relevant files that might be related:
const env = require('./env-config.js');
module.exports = {
babelrc: false,
plugins: [
['emotion', { autoLabel: true, sourceMap: true }],
'inline-react-svg',
['transform-define', env],
['lodash'],
],
env: {
development: {
presets: ['next/babel'],
},
production: {
presets: ['next/babel', ['@babel/env', { targets: { node: '8' } }]],
plugins: [['emotion', { autoLabel: true, sourceMap: false }]],
},
test: {
presets: [['next/babel', { 'preset-env': { modules: 'commonjs' } }]],
},
},
};
const withBundleAnalyzer = require('@zeit/next-bundle-analyzer');
const withProgressBar = require('next-progressbar');
const withOffline = require('next-offline');
const serviceWorkerConfig = require('./src/lib/serviceWorker/config');
module.exports = withOffline(
withBundleAnalyzer(
withProgressBar({
assetPrefix: '',
useFileSystemPublicRoutes: false,
poweredByHeader: false,
webpack: config => {
config.module.rules.push(
{
test: /\.css$/,
use: ['to-string-loader', 'css-loader'],
},
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
},
}
);
return config;
},
analyzeServer: ['server', 'both'].includes(process.env.BUNDLE_ANALYZE),
analyzeBrowser: ['browser', 'both'].includes(process.env.BUNDLE_ANALYZE),
bundleAnalyzerConfig: {
server: {
analyzerMode: 'static',
reportFilename: '../../bundles/server.html',
},
browser: {
analyzerMode: 'static',
reportFilename: '../bundles/client.html',
},
},
workboxOpts: serviceWorkerConfig,
})
)
);
{
"name": "next-test",
"main": "index.js",
"scripts": {
"dev": "NODE_ENV=development nodemon"
},
"engines": {
"node": "10.15.0",
"npm": "6.4.1"
},
"dependencies": {
"@yolkai/next-routes": "^2.0.0",
"@zeit/next-bundle-analyzer": "^0.1.2",
"express": "^4.16.4",
"next": "^8.0.3",
"next-offline": "^3.3.3",
"next-progressbar": "^1.0.0",
"react": "^16.8.3",
"react-dom": "^16.8.3"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/node": "^7.0.0",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"ajv": "^6.7.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.4.2",
"babel-plugin-emotion": "^10.0.6",
"babel-plugin-inline-react-svg": "^1.0.1",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-transform-define": "^1.3.1",
"nodemon": "^1.18.9"
}
}
Does anyone have any insight? Or an example project with a transpiled server we can build backward from? Being able to write our code the same everywhere is a huge boon, but these transpilation issues make it seem like a zero sum game. |
Just did a new release that holds #6534, currently on it's way to being published to npm as next@canary. |
I tested
.babelrc.js
|
@schoenwaldnils that's actually a different issue: #6240 |
add comment lineE2-lazy-loading-modules/node_modules/next/dist/client/next-dev.js |
That won't solve the issue if you have dynamic imports in other places. |
There are so many invalid babelrc code snippets on this thread. |
Unfortunately, I've run into a similar issue.
This only happens when Then I try running
I've tried a few of the suggestions found here but without much success. Has the issue been directly addressed? |
Is there any solution for this? How can I run |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
I am getting the following issue which I presume is related to babel.
It seems that
@babel/runtime-corejs2/helpers/esm
is being used instead of just@babel/runtime-corejs2/helpers
where the compiled files seem to be.I didn't notice this in
dev
with the following configThe issue is when I try and run the production build. Here is the relevant config
The text was updated successfully, but these errors were encountered: