question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't import to React

See original GitHub issue

Hi, I am not able to import shiki into a react app. I am getting Uncaught TypeError: fs.readFileSync is not a function error. Here’s my code for reference.

import './App.css'
import { useEffect, useState } from 'react'
import  shiki  from 'shiki'

shiki.getHighlighter({
  theme: 'nord'
}).then(highlighter => {
  console.log(highlighter.codeToHtml(`console.log('shiki');`, 'js'))
})
...

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
antfucommented, Feb 5, 2021

@i9or Thanks for the detailed description and repro.

For 2. and 3. should be fixed in the recent commits.

For 1. You can use your current solution or via third-party CDNs like unpkg or jsdelivr. If you’d like to bundle those file, you can do them this way:

import * as shiki from 'shiki';
import themeNord from 'shiki/themes/nord.json'
import langTS from 'shiki/languages/typescript.tmLanguage.json'
import langTSX from 'shiki/languages/tsx.tmLanguage.json'
import onigasm from 'arraybuffer-loader!shiki/dist/onigasm.wasm'

shiki.setOnigasmWASM(onigasm)
const highlighter = await shiki.getHighlighter({
  theme: 'nord',
  themes: [themeNord],
  langs: [{
    id: 'typescript',
    scopeName: langTS.scopeName,
    grammar: langTS,
    aliases: ['ts']
  }, {
    id: 'tsx',
    scopeName: langTSX.scopeName,
    grammar: langTSX
  }],
})
const highlightedCode = highlighter.codeToHtml(SAMPLE_CODE, 'tsx');
1reaction
ortacommented, Feb 5, 2021

I can, and I have - this is shipped in 0.9.2 👍🏻

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot import react js component - Stack Overflow
I post simpler version which I know does work: ./index.js : import React from 'react'; import ReactDOM from 'react-dom'; import Application ...
Read more >
Can't use import React from 'react' · Issue #543 - GitHub
I'm newbie in react. I'm using this gem with a rails app, using es6 and jsx. In assets/javascripts/components/message.es6.jsx class Message ...
Read more >
Importing Components in React From Other Files - Upmostly
There is a key import limitation in React which we haven't discussed thus far: you cannot import from files outside the src directory....
Read more >
Module not found: Can't resolve 'X' error in React | bobbyhadz
The error "Module not found: Can't resolve" occurs for multiple reasons in React: Importing a local file from an incorrect relative path.
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found