最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting SyntaxError when using lightweight-charts in NextJS - Stack Overflow

programmeradmin1浏览0评论

I'm trying to use the lightweight-charts package in my nextjs project, however when i try to call the createChart function I get this error in my nodejs console.

...\lightweight-charts\dist\lightweight-charts.esm.development.js:7
import { bindToDevicePixelRatio } from 'fancy-canvas/coordinate-space';
^^^^^^

SyntaxError: Cannot use import statement outside a module

Component:

import styled from "styled-ponents"
import { createChart } from 'lightweight-charts';

const Wrapper = styled.div``

const CoinPriceChart = () => {
  const chart = createChart(document.body, { width: 400, height: 300 });
  return <Wrapper></Wrapper>
}

export default CoinPriceChart

Page:

import styled from "styled-ponents"
import CoinPriceChart from "../../ponents/charts/CoinPriceChart"

const Wrapper = styled.div``

const CoinDetailPage = () => {
  return (
    <Wrapper>
      <CoinPriceChart />
    </Wrapper>
  )
}

export default CoinDetailPage

Does someone have an idea what I could do to enable me to use the library within nextjs? Thank you!

I'm trying to use the lightweight-charts package in my nextjs project, however when i try to call the createChart function I get this error in my nodejs console.

...\lightweight-charts\dist\lightweight-charts.esm.development.js:7
import { bindToDevicePixelRatio } from 'fancy-canvas/coordinate-space';
^^^^^^

SyntaxError: Cannot use import statement outside a module

Component:

import styled from "styled-ponents"
import { createChart } from 'lightweight-charts';

const Wrapper = styled.div``

const CoinPriceChart = () => {
  const chart = createChart(document.body, { width: 400, height: 300 });
  return <Wrapper></Wrapper>
}

export default CoinPriceChart

Page:

import styled from "styled-ponents"
import CoinPriceChart from "../../ponents/charts/CoinPriceChart"

const Wrapper = styled.div``

const CoinDetailPage = () => {
  return (
    <Wrapper>
      <CoinPriceChart />
    </Wrapper>
  )
}

export default CoinDetailPage

Does someone have an idea what I could do to enable me to use the library within nextjs? Thank you!

Share Improve this question asked Apr 12, 2021 at 13:18 marcell-janovszkimarcell-janovszki 1177 bronze badges 1
  • Does this help answer your question NextJS + react-hook-mousetrap : “Cannot use import statement outside a module”? Different lib but same solution. – juliomalves Commented Apr 12, 2021 at 20:41
Add a ment  | 

1 Answer 1

Reset to default 9

That because you are trying to import the library in SSR context. Using next.js Dynamic with ssr : false should fix the issue :

import styled from "styled-ponents"
import dynamic from "next/dynamic";
const CoinPriceChart = dynamic(() => import("../../ponents/charts/CoinPriceChart"), {
  ssr: false
});

const Wrapper = styled.div``

const CoinDetailPage = () => {
  return (
    <Wrapper>
      <CoinPriceChart />
    </Wrapper>
  )
}

export default CoinDetailPage
发布评论

评论列表(0)

  1. 暂无评论