I am currently working on developing an online board app using React, Next.js, and TypeScript as a project for my portfolio. This is my first time using TypeScript.
However, I encountered an issue with importing roughjs into canvas.tsx.
Below is the code from canvas.tsx:
import React, {useLayoutEffect} from 'react'
import rough from 'roughjs/bundled/rough.esm'
type Props = {
backgroundColor?: string
}
const generator = rough.generator()
const Canvas = ({backgroundColor = '#ffffff'}: Props) => {
useLayoutEffect(() => {
const canvas = document.getElementById('canvas') as HTMLCanvasElement
const ctx = canvas.getContext('2d')
const roughCanvas = rough.canvas(canvas)
})
return (
<canvas
id="canvas"
style={{backgroundColor: backgroundColor}}
className="h-screen w-screen"
></canvas>
)
}
export default Canvas
When testing in the browser, I received a
SyntaxError: Unexpected token 'export'
.
In addition, VS Code also showed an error in the import statement
import rough from 'roughjs/bundled/rough.esm'
:
Could not find a declaration file for module 'roughjs/bundled/rough.esm'. 'C:/Stepan/online board/website/node_modules/.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0829f8597989a83b0c4dec5dec2">[email protected]</a>/node_modules/roughjs/bundled/rough.esm.js' implicitly has an 'any' type. Try 'npm i --save-dev @types/roughjs' if it exists or add a new declaration (.d.ts) file containing 'declare module 'roughjs/bundled/rough.esm';'
Here is the content of my json file:
{
"type": "module",
"name": "online_board",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "12.1.6",
"perfect-freehand": "^1.1.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"roughjs": "^4.5.2"
},
"devDependencies": {
"@types/node": "17.0.44",
"@types/react": "18.0.12",
"@types/react-dom": "18.0.5",
"autoprefixer": "^10.4.7",
"eslint": "8.17.0",
"eslint-config-next": "12.1.6",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.4",
"typescript": "4.7.3"
}
}
I have tried various solutions found online but none seem to work. Here are some of the resources I looked at:
https://github.com/rough-stuff/rough/issues/145
Getting Unexpected Token Export
You can check out the GitHub repository for this project here: