hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39.28k stars 4.83k forks source link

Module parse failed: Unexpected character '�' (1:0) #5379

Open Redish101 opened 9 months ago

Redish101 commented 9 months ago

Check List

Expected behavior

Use hexo api in a nextjs project.

Actual behavior

It threw "Module parse failed: Unexpected character '�' (1:0)" in this code

import Hexo from "hexo";
import path from "path";
import { cache } from "react";

const initHexo = cache(async () => {
    const hexo = new Hexo()
    await hexo.init()
})

export {
    initHexo
}

How to reproduce?

  1. Create a next.js project
  2. Use hexo api in a nextjs page.

Is the problem still there under Safe mode?

Yes

Your Node.js & npm version

v18.16.0
9.5.1

Your Hexo and Plugin version

├── @eslint-community/eslint-utils@4.4.0 extraneous -> ./node_modules/.pnpm/@eslint-community+eslint-utils@4.4.0_eslint@8.44.0/node_modules/@eslint-community/eslint-utils
├── @eslint-community/regexpp@4.5.1 extraneous -> ./node_modules/.pnpm/@eslint-community+regexpp@4.5.1/node_modules/@eslint-community/regexpp
├── @eslint/eslintrc@2.1.0 extraneous -> ./node_modules/.pnpm/@eslint+eslintrc@2.1.0/node_modules/@eslint/eslintrc
├── @eslint/js@8.44.0 extraneous -> ./node_modules/.pnpm/@eslint+js@8.44.0/node_modules/@eslint/js
├── @next/eslint-plugin-next@13.4.9 extraneous -> ./node_modules/.pnpm/@next+eslint-plugin-next@13.4.9/node_modules/@next/eslint-plugin-next
├── @retalkgo/client@0.1.0-alpha.9 -> ./node_modules/.pnpm/@retalkgo+client@0.1.0-alpha.9/node_modules/@retalkgo/client
├── @rushstack/eslint-patch@1.3.2 extraneous -> ./node_modules/.pnpm/@rushstack+eslint-patch@1.3.2/node_modules/@rushstack/eslint-patch
├── @types/eslint-scope@3.7.4 extraneous -> ./node_modules/.pnpm/@types+eslint-scope@3.7.4/node_modules/@types/eslint-scope
├── @types/marked@5.0.0 -> ./node_modules/.pnpm/@types+marked@5.0.0/node_modules/@types/marked
├── @types/md5@2.3.4 -> ./node_modules/.pnpm/@types+md5@2.3.4/node_modules/@types/md5
├── @types/node@20.4.1 -> ./node_modules/.pnpm/@types+node@20.4.1/node_modules/@types/node
├── @types/nprogress@0.2.0 -> ./node_modules/.pnpm/@types+nprogress@0.2.0/node_modules/@types/nprogress
├── @types/prismjs@1.26.0 -> ./node_modules/.pnpm/@types+prismjs@1.26.0/node_modules/@types/prismjs
├── @types/react-dom@18.2.6 -> ./node_modules/.pnpm/@types+react-dom@18.2.6/node_modules/@types/react-dom
├── @types/react@18.2.14 -> ./node_modules/.pnpm/@types+react@18.2.14/node_modules/@types/react
├── @typescript-eslint/scope-manager@5.61.0 extraneous -> ./node_modules/.pnpm/@typescript-eslint+scope-manager@5.61.0/node_modules/@typescript-eslint/scope-manager
├── @typescript-eslint/types@5.61.0 extraneous -> ./node_modules/.pnpm/@typescript-eslint+types@5.61.0/node_modules/@typescript-eslint/types
├── @typescript-eslint/visitor-keys@5.61.0 extraneous -> ./node_modules/.pnpm/@typescript-eslint+visitor-keys@5.61.0/node_modules/@typescript-eslint/visitor-keys
├── @vercel/analytics@1.0.1 -> ./node_modules/.pnpm/@vercel+analytics@1.0.1/node_modules/@vercel/analytics
├── eslint-config-next@13.4.9 -> ./node_modules/.pnpm/eslint-config-next@13.4.9_eslint@8.44.0_typescript@5.1.6/node_modules/eslint-config-next
├── eslint-import-resolver-node@0.3.7 extraneous -> ./node_modules/.pnpm/eslint-import-resolver-node@0.3.7/node_modules/eslint-import-resolver-node
├── eslint-module-utils@2.8.0 extraneous -> ./node_modules/.pnpm/eslint-module-utils@2.8.0_@typescript-eslint+parser@5.61.0_eslint-import-resolver-node@0.3.7__nkp6ntjfycjq6yp5liyvdnrfxa/node_modules/eslint-module-utils
├── eslint-plugin-react-hooks@5.0.0-canary-7118f5dd7-20230705 extraneous -> ./node_modules/.pnpm/eslint-plugin-react-hooks@5.0.0-canary-7118f5dd7-20230705_eslint@8.44.0/node_modules/eslint-plugin-react-hooks
├── eslint@8.44.0 -> ./node_modules/.pnpm/eslint@8.44.0/node_modules/eslint
├── gray-matter@4.0.3 -> ./node_modules/.pnpm/gray-matter@4.0.3/node_modules/gray-matter
├── hexo@7.0.0 -> ./node_modules/.pnpm/hexo@7.0.0/node_modules/hexo
├── highlight.js@11.8.0 -> ./node_modules/.pnpm/highlight.js@11.8.0/node_modules/highlight.js
├── highlightjs@9.16.2 -> ./node_modules/.pnpm/highlightjs@9.16.2/node_modules/highlightjs
├── marked@5.1.1 -> ./node_modules/.pnpm/marked@5.1.1/node_modules/marked
├── md5@2.3.0 -> ./node_modules/.pnpm/md5@2.3.0/node_modules/md5
├── moment@2.29.4 -> ./node_modules/.pnpm/moment@2.29.4/node_modules/moment
├── next@13.4.10 -> ./node_modules/.pnpm/next@13.4.10_@babel+core@7.22.9_react-dom@18.2.0_react@18.2.0/node_modules/next
├── nprogress@0.2.0 -> ./node_modules/.pnpm/nprogress@0.2.0/node_modules/nprogress
├── react-dom@18.2.0 -> ./node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom
├── react@18.2.0 -> ./node_modules/.pnpm/react@18.2.0/node_modules/react
├── remark@14.0.3 -> ./node_modules/.pnpm/remark@14.0.3/node_modules/remark
├── style9-webpack@0.5.0 -> ./node_modules/.pnpm/style9-webpack@0.5.0_eslint-plugin-import@2.27.5_eslint@8.44.0_style9@0.18.2/node_modules/style9-webpack
├── style9@0.18.2 -> ./node_modules/.pnpm/style9@0.18.2_rollup@2.79.1_webpack@5.88.1/node_modules/style9
└── typescript@5.1.6 -> ./node_modules/.pnpm/typescript@5.1.6/node_modules/typescript

Your package.json

{
  "name": "blog-next",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@retalkgo/client": "0.1.0-alpha.9",
    "@types/marked": "^5.0.0",
    "@types/md5": "^2.3.4",
    "@types/node": "20.4.1",
    "@types/nprogress": "^0.2.0",
    "@types/prismjs": "^1.26.0",
    "@types/react": "18.2.14",
    "@types/react-dom": "18.2.6",
    "@vercel/analytics": "^1.0.1",
    "eslint": "8.44.0",
    "eslint-config-next": "13.4.9",
    "gray-matter": "^4.0.3",
    "hexo": "^7.0.0",
    "highlight.js": "^11.8.0",
    "highlightjs": "^9.16.2",
    "marked": "^5.1.1",
    "md5": "^2.3.0",
    "moment": "^2.29.4",
    "next": "13.4.10",
    "nprogress": "^0.2.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "remark": "^14.0.3",
    "style9": "^0.18.2",
    "style9-webpack": "^0.5.0",
    "typescript": "5.1.6"
  }
}

Your site's _config.yml (Optional)

# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site
title: Hexo
subtitle: ''
description: ''
keywords:
author: John Doe
language: en
timezone: ''

# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: http://example.com
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
  trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
  trailing_html: true # Set to false to remove trailing '.html' from permalinks

# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:

# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link:
  enable: true # Open external links in new tab
  field: site # Apply to the whole site
  exclude: ''
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
syntax_highlighter: highlight.js
highlight:
  line_number: true
  auto_detect: false
  tab_replace: ''
  wrap: true
  hljs: false
prismjs:
  preprocess: true
  line_number: true
  tab_replace: ''

# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
  path: ''
  per_page: 10
  order_by: -date

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Metadata elements
## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
meta_generator: true

# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
## updated_option supports 'mtime', 'date', 'empty'
updated_option: 'mtime'

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Include / Exclude file(s)
## include:/exclude: options only apply to the 'source/' folder
include:
exclude:
ignore:

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
  type: ''

Others

No response

ChrAlpha commented 7 months ago

If this error is threw by something like fsevents.node and you're using macOS, maybe you should configure node-loader as webpack's loader for node files. It's wired since everything just run expectively correctly on Ubuntu.

Especially for Next.js project. Try adding following configuration to next.config.js:

  webpack: (config, options) => {
    config.module.rules.push({
      test: /\.node/,
      loader: "node-loader",
    });

    return config;
  }