[英]Typescript and nextjs: SyntaxError: Unexpected token 'export'
I'm trying to develop online board app(project for portfolio) with react, nextjs, typescript.我正在尝试使用 react、nextjs、typescript 开发在线板应用程序(投资组合项目)。 (using typescript for the first time)
(第一次使用打字稿)
So I have problem with importing roughjs into canvas.tsx .所以我在将 roughjs 导入 canvas.tsx 时遇到问题。
Here is canvas.tsx :这是 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
In browser I have SyntaxError: Unexpected token 'export'
.在浏览器中,我有
SyntaxError: Unexpected token 'export'
。
And VS code also giving me this error in import rough from 'roughjs/bundled/rough.esm'
: VS 代码在
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/roughjs@4.5.2/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 my json file:这是我的 json 文件:
{
"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"
}
}
Please help me, I tried all I could find on the Internet, nothing helped.请帮助我,我在互联网上尝试了所有我能找到的东西,没有任何帮助。
What I tried:我尝试了什么:
https://github.com/rough-stuff/rough/issues/145 https://github.com/rough-stuff/rough/issues/145
Getting Unexpected Token Export 获得意外的令牌导出
here is github code of this project:这是这个项目的github代码:
https://github.com/pan-grayza/School-Online-Board https://github.com/pan-grayza/School-Online-Board
The module is not bundled with type declarations.该模块不与类型声明捆绑在一起。 In this case you can switch to commonjs require syntax and it should work:
在这种情况下,您可以切换到 commonjs require 语法,它应该可以工作:
const rough = require('roughjs/bundled/rough.cjs')
Also, you are not writing anything to your rough canvas.此外,您不会在粗糙的画布上写任何东西。 To write you can do something like that:
要编写,您可以执行以下操作:
const roughCanvas = rough.canvas(canvas)
roughCanvas.circle(80, 120, 50);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.