You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloudflare Pages部署Remix时nodejs_compat失效,无法解析crypto模块

Remix + Cloudflare Pages 集成 jsonwebtoken 时无法解析 crypto/stream 模块的问题

我用Remix官方Cloudflare模板部署到Cloudflare Pages,原本流程正常,但添加jsonwebtoken包后,出现无法解析crypto和stream模块的错误。

已在wrangler.toml和Cloudflare控制台中添加了nodejs_compat兼容标志,但由于这些模块是jsonwebtoken内部调用的,无法手动添加node:前缀。尝试设置Cloudflare中的NODE_VERSION也未解决问题。

错误信息

00:48:56.191    Detected the following tools from environment: nodejs@20.5.1, npm@9.6.7
00:48:56.192    Installing nodejs 20.5.1
00:48:56.266    nodejs 20.5.1 is already installed
...
00:49:31.771    ✘ [ERROR] 6 error(s) and 0 warning(s) when compiling Worker.
00:49:31.772    
00:49:31.772    
00:49:31.775    
00:49:31.777    ✘ [ERROR] Could not resolve "crypto"
00:49:31.777    
00:49:31.777        ../node_modules/jsonwebtoken/sign.js:12:65:
00:49:31.777          12 │ ...KeyObject, createSecretKey, createPrivateKey } = require('crypto')
00:49:31.777             ╵                                                             ~~~~~~~~
00:49:31.777    
00:49:31.777      The package "crypto" wasn't found on the file system but is built into node.
00:49:31.777      Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "crypto"
23:55:39.338    
23:55:39.338        ../node_modules/jsonwebtoken/sign.js:12:65:
23:55:39.338          12 │ ...KeyObject, createSecretKey, createPrivateKey } = require('crypto')
23:55:39.338             ╵                                                             ~~~~~~~~
23:55:39.339    
23:55:39.339      The package "crypto" wasn't found on the file system but is built into node.
23:55:39.339      Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.

当前项目配置

package.json

{
    "name": "name",
    "private": true,
    "sideEffects": false,
    "type": "module",
    "scripts": {
        "build": "remix vite:build",
        "deploy": "wrangler pages deploy ./build/client",
        "dev": "remix vite:dev",
        "lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
        "start": "wrangler pages dev ./build/client",
        "typecheck": "tsc",
        "typegen": "wrangler types"
    },
    "dependencies": {
        "@radix-ui/react-dialog": "^1.1.1",
        "@radix-ui/react-label": "^2.1.0",
        "@radix-ui/react-slot": "^1.1.0",
        "@remix-run/cloudflare": "^2.11.2",
        "@remix-run/cloudflare-pages": "^2.11.2",
        "@remix-run/react": "^2.11.2",
        "class-variance-authority": "^0.7.0",
        "clsx": "^2.1.1",
        "framer-motion": "^11.3.30",
        "isbot": "^4.1.0",
        "jsonwebtoken": "^9.0.2",
        "lucide-react": "^0.435.0",
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "tailwind-merge": "^2.5.2",
        "tailwindcss-animate": "^1.0.7"
    },
    "devDependencies": {
        "@cloudflare/workers-types": "^4.20240512.0",
        "@remix-run/dev": "^2.11.2",
        "@tailwindcss/typography": "^0.5.14",
        "@types/jsonwebtoken": "^9.0.6",
        "@types/react": "^18.2.20",
        "@types/react-dom": "^18.2.7",
        "@typescript-eslint/eslint-plugin": "^6.7.4",
        "@typescript-eslint/parser": "^6.7.4",
        "autoprefixer": "^10.4.19",
        "eslint": "^8.38.0",
        "eslint-import-resolver-typescript": "^3.6.1",
        "eslint-plugin-import": "^2.28.1",
        "eslint-plugin-jsx-a11y": "^6.7.1",
        "eslint-plugin-react": "^7.33.2",
        "eslint-plugin-react-hooks": "^4.6.0",
        "postcss": "^8.4.38",
        "tailwindcss": "^3.4.4",
        "typescript": "^5.1.6",
        "vite": "^5.1.0",
        "vite-tsconfig-paths": "^4.2.1",
        "wrangler": "3.57.1"
    },
    "engines": {
        "node": ">=20.0.0"
    }
}

wrangler.toml

compatibility_flags = ["nodejs_compat"]

[[services]]
binding = "DB"    # name of the binding in `env`
service = "another-worker"    # name of the worker in the dashboard

jsonwebtoken导入方式

// auth.ts
import jwt from 'jsonwebtoken'

请问该如何解决?


内容的提问来源于stack exchange,提问作者Gabriel JC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 15:33:11