Next.JS 12 Module parse failed: Unexpected token (x:y)
Verify canary release
- [X] I verified that the issue exists in the latest Next.js canary release
Provide environment information
"react": "^18.2.0",
"next": "^12.2.5",
"react-dom": "^18.2.0",
"rollup": "^2.79.0",
"rollup-plugin-ts": "^3.0.2",
"ts-loader": "^9.3.1",
"ts-node": "^10.9.1",
"typescript": "^4.7.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
yarn build yarn start
Describe the Bug
I have 2 modules in a mono-repo with 2 web applications using ReactJS, NextJS:
/project
|- api/
|- web/
next.config.js:
const aliases = {
api: path.join(__dirname, '../api/'),
}
module.exports = {
reactStrictMode: true,
swcMinify: true,
experimental: {
forceSwcTransforms: true,
swcTraceProfiling: true,
},
output: 'standalone',
i18n: {
defaultLocale: 'us',
locales: ['us', 'sg', 'my', 'th', 'vn', 'id', 'de'],
},
webpack: (config) => {
// Add aliases
config.resolve.alias = {
...(config.resolve.alias || {}),
...aliases,
}
// Find rules that includes current directory
const rulesWithCurrentDir = config.module.rules.filter((rule) => rule.include && rule.include.includes(dirname))
// Prepare the sibling package paths that we want to include
const siblingPackagePaths = [path.resolve(`${dirname}../api`)]
// Push sibling package paths
rulesWithCurrentDir.forEach((ruleWithCurrentDir) => ruleWithCurrentDir.include.push(...siblingPackagePaths))
return config
},
}
webpack.config.js:
import path from 'path';
module.exports = {
mode: "development",
// Change to your "entry-point".
entry: './index',
output: {
path: path.resolve(__dirname, '.next'),
filename: 'app.bundle.js'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json']
},
module: {
rules: [
{
test: /\.(css)$/,
include: [/stylesheets/, /node_modules/],
use: [
'css-loader',
],
},
{
test: /\.css$/,
exclude: [/stylesheets/, /node_modules/],
use: [
'css-loader?sourceMap&modules,localIdentName=[local]-[hash:base64]',
],
},
{
test: /\.(ts|tsx)?$/,
include: path.resolve(__dirname, '**/*'),
exclude: /node_modules/,
loader: 'ts-loader',
},
{
test: /\.(ts|tsx)?$/,
include: path.resolve(__dirname, '../api/**/*'),
exclude: /node_modules/,
loader: 'ts-loader',
},
],
}
};
.babelrc:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
tsconfig.json:
{
"extends": "../../tsconfig.json",
"exclude": [
"node_modules",
"build"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.js",
"**/*.tsx",
"**/*.json"
],
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
],
"extensions": [
"ts",
"tsx"
],
"compilerOptions": {
"baseUrl": ".",
"outDir": "build/",
"incremental": true,
"target": "es2022",
"lib": [
"dom",
"dom.iterable",
"es2022"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"plugins": [
{
"name": "@rollup/plugin-typescript"
},
{
"name": "@rollup/plugin-json"
}
],
"noEmit": true,
"jsx": "preserve"
},
"references": [
{
"path": "../api"
}
]
}
next.config.mjs:
export default {
swcMinify: true,
experimental: {
forceSwcTransforms: true,
swcTraceProfiling: true,
},
output: 'standalone',
};
next build errors:
../web/src/views/Dashboard/Bids/BidDetailsDescriptionTable.tsx
Module parse failed: Unexpected token (58:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| const { subtotal, shipping } = pricing || {}
| return (
> <Root>
| <Hidden mdDown>
| <Table className={classes.tableBody}>
../web/src/views/Dashboard/Bids/QuotationDetails/QuotationSlip.tsx
Module parse failed: Unexpected token (65:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| const { sellerSignature, sellerSigneeName, sellerSigneeDesignation, quotationTimeStamp } = quotationBid || {}
| return (
> <Root className={classes.boxWrap}>
| <SharedSlipDetails titleType="Quotation" bidStatusTimeStamp={QUOTATION_TIME_STAMP} quotationBid={quotationBid} />
| <BidDetailsDescriptionTable quotationBid={quotationBid} />
../web/src/views/Dashboard/Bids/SharedSlipDetails.tsx
Module parse failed: Unexpected token (69:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| return (
> <Root>
| <Box display="flex">
| <img src="/images/kyberlife-bid-form-logo.png" alt="logo" className={classes.imgSize} />
../web/src/views/Dashboard/Messages/Messages.tsx
Module parse failed: Unexpected token (194:27)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| // Getters
> const getInitialChats = (): ChatInterface[] => {
| const isNewChatAlreadyExists =
| hasNewChat && prevChats?.find((prevChat) => prevChat.participantIDs.includes(query.sellerID as string))
../web/src/views/Dashboard/Messages/utils.ts
Module parse failed: Unexpected token (9:42)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { SellerInterface } from '@app/api/src/modules/Seller/model/Seller'
|
> export const getUnreadMessageCount = (chat: ChatInterface, userID: string): number => {
| const unReadMessages =
| chat?.messages?.filter(({ isReadIDs }) => !isReadIDs || !isReadIDs.includes(userID))?.length || 0
I only see this error after upgrading from Next.JS 11 to 12.
Expected Behavior
No build error
Link to reproduction
https://stackoverflow.com/questions/73563822/reactjs-next-build-module-parse-failed-unexpected-token-5452
To Reproduce
Install next.js 12, run yarn build
Would you like to provide a reproduction of your issue? It seems that you are not using Next.js (Next.js itself does not require the rollup to be installed or webpack.config.js to exist).
I didn't have webpack.config.js initially. This config seems redundant and removing it doesn't solve this build error.
I think my issue is related to this:
../../../packages/rjsf-px-ui/src/SelectWidget/arrow-down.react.svg
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <svg fill="none" viewBox="0 0 20 12" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M10 12 0 2 1.414.586l10 10L10 12Z" /><path fill-rule="evenodd" clip-rule="evenodd" d="M20 2 10 12l-1.414-1.414 10-10L20 2Z" /></svg>
|
Doesn't happen on 12.1.5 but when upgrading to 12.2.5 I get the issue.
next.config.js:
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path')
const withNx = require('@nrwl/next/plugins/with-nx')
try {
require('dotenv').config()
} catch (e) {
// noop
}
/**
* @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
**/
const nextConfig = {
nx: {
// Set this to true if you would like to to use SVGR
// See: https://github.com/gregberge/svgr
svgr: false,
},
webpack: config => {
config.module.rules.push({
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
include: [path.resolve(__dirname, '../../../../node_modules'), __dirname],
oneOf: [
{
issuer: /\.(js|ts|md)x?$/,
// In order to import the svg as a React element add the .react to the file name e.g. MyIcon.react.svg
include: [/\.react\.svg$/],
use: ['babel-loader', '@svgr/webpack'],
},
{
issuer: /\.(js|ts|md)x?$/,
use: ['url-loader'],
},
],
})
config.resolve.fallback = {
url: require.resolve('url'),
events: require.resolve('events'),
https: require.resolve('https-browserify'),
http: require.resolve('stream-http'),
}
return config
},
serverRuntimeConfig: {
platformApiHost: process.env.PLATFORM_API_HOST,
},
swcMinify: false,
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
module.exports = withNx(nextConfig)
Same issue here with next 13.1.1 Stackoverflow Issue
my next config
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
compress: true,
transpilePackages: ['ui']
}
module.exports = withNextPwa(withBundleAnalyzer(
withSentryConfig({
nextConfig,
sentryWebpackPluginOptions,
})
));
getting below error
../../packages/ui/components/Button.tsx
Module parse failed: Unexpected token (4:28)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
import { ButtonProps } from "../type";
|
> export const Button = (props:ButtonProps) => {
| return <button>{props.label}</button>
| }
without the plugin working perfectly.
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
poweredByHeader: false,
compress: true,
transpilePackages: ['ui']
}
module.exports = nextConfig
Please verify that your issue can be recreated with next@canary.
Why was this issue marked with the please verify canary label?
We noticed the provided reproduction was using an older version of Next.js, instead of canary.
The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running npm install next@canary and test it in your project, using your reproduction steps.
If the issue does not reproduce with the canary version, then it has already been fixed and this issue can be closed.
How can I quickly verify if my issue has been fixed in canary?
The safest way is to install next@canary in your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases. You can also use the GitHub templates (preferred) for pages and App Router, or the CodeSandbox: pages or CodeSandbox: App Router templates to create a reproduction with canary from scratch.
My issue has been open for a long time, why do I need to verify canary now?
Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.
What happens if I don't verify against the canary version of Next.js?
An issue with the please verify canary that receives no meaningful activity (e.g. new comments that acknowledge verification against canary) will be automatically closed and locked after 30 days.
If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using next@canary.
I did not open this issue, but it is relevant to me, what can I do to help?
Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the :+1: reaction on the topmost comment (please do not comment "I have the same issue" without reproduction steps). Then, we can sort issues by votes to prioritize.
I think my reproduction is good enough, why aren't you looking into it quicker?
We look into every Next.js issue and constantly monitor open issues for new comments.
However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.
Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.
Useful Resources
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.