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
Module parse failed: Unexpected token (401:22) #1723
Comments
You did not provide any info about your environment. You may be using Webpack, but I'm not sure. Can you provide a reproducible example? |
when I even just install react-pdf and add the above app.js code in app.js file I am getting this reproduced
"react-scripts": "4.0.3", |
You may be affected by old version of Webpack not supporting nullish coalescing ( |
Upgrading Webpack is the only solution we got? |
Updating Babel is worth a shot too, but it doesn't look too good. Upgrading Webpack may be your best choice. What's blocking you exactly? You could also try a legacy PDF.js build or an older React-PDF version. Try v5? 🤔 But remember that then you're digging a deeper hole for yourself, so I'd use this as a last resort. |
I added a webpack.config.js file and babel.config.js file by adding those 2 babels (@babel/plugin-proposal-optional-chaining and @babel/plugin-proposal-nullish-coalescing-operator) mentioned in the linked solution by you. can you please tell which react-pdf version can i use with "react-scripts": "4.0.3" and react 17 version |
Have you tried v5 as i suggested? |
Yes, I tried with version "react-pdf": "^5.7.2", here is the webpack.config.js file :
here is the babel.config.json file :
this is the error :
|
The error does not come from react-pdf, but react-data-grid. Granted though, it's exactly the same problem you started the issue with. Here are the facts:
You will need to parse files with Babel in order to get old Webpack to work. Try removing
bit. It will be painfully slow, but chances are it might work. If that's so, you can then add Once again, you're gonna have a real bad time with outdated Webpack version, and as you can clearly see from the error above, it's not gonna be react-pdf exclusive. |
This comment has been minimized.
This comment has been minimized.
I am using node version 21.16.1 { Can I get help in resolving this? |
Before you start - checklist
Description
my dependencies -
"react-scripts": "4.0.3",
"react": "^17.0.2",
"react-pdf": "^7.7.0",
I am getting error :
1. Module not found
import "react-pdf/dist/Page/AnnotationLayer.css";
import "react-pdf/dist/Page/TextLayer.css";
2. Module parse failed: Unexpected token (401:22)
./node_modules/pdfjs-dist/build/pdf.js 401:22
Module parse failed: Unexpected token (401:22)
File was processed with these loaders:
You may need an additional loader to handle the result of these loaders.
|
| function _isValidProtocol(url) {
| case "http:":
| case "https:":
Steps to reproduce
install react-pdf and add this code in app.js
import React, { useState, useEffect } from "react";
import { pdfjs, Document, Page } from "react-pdf";
import "react-pdf/dist/Page/AnnotationLayer.css";
import "react-pdf/dist/Page/TextLayer.css";
pdfjs.GlobalWorkerOptions.workerSrc =
//unpkg.com/pdfjs-dist@${pdfjs.version}/legacy/build/pdf.worker.min.js
;function App() {
const [numPages, setNumPages] = useState(null);
const [pageNumber, setPageNumber] = useState(1);
useEffect(() => {
const loadWords = async () => {
const words = await getPageTextContent(pageNumber);
console.log(
"Indices of words on first load:",
words.map((word, index) => ({ word, index }))
);
};
loadWords();
}, []);
const onDocumentLoadSuccess = ({ numPages }) => {
setNumPages(numPages);
};
const handleMouseDown = async (event) => {
const selection = window.getSelection();
if (selection) {
const selectedText = selection.toString();
const range = selection.getRangeAt(0);
const boundingRect = range.getBoundingClientRect();
};
const getPageTextContent = async (pageNum) => {
const loadingTask = pdfjs.getDocument("test07.pdf");
const pdf = await loadingTask.promise;
const page = await pdf.getPage(pageNum);
const textContent = await page.getTextContent();
};
const onPageLoadSuccess = (page) => {
console.log(page);
console.log("Page width:", page?.viewport?.width);
console.log("Page height:", page?.viewport?.height);
};
return (
<Page
pageNumber={pageNumber}
onMouseDown={handleMouseDown}
onLoadSuccess={onPageLoadSuccess}
scale={1}
canvasLayer={(canvas, pageProps) => {
const { width, height } = pageProps.viewport;
const context = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
);
}
export default App;
Expected behavior
need the pdf view
and to console x and y , width and height of selected text
along with the number of the occurrence if it is repeating
Actual behavior
Its throwing error and server is not starting up :
./node_modules/pdfjs-dist/build/pdf.js 401:22
Module parse failed: Unexpected token (401:22)
File was processed with these loaders:
You may need an additional loader to handle the result of these loaders.
|
| function _isValidProtocol(url) {
| case "http:":
| case "https:":
Additional information
No response
Environment
The text was updated successfully, but these errors were encountered: