napi-rs / node-rs

Node.js bindings ❤️ Rust crates
https://node-rs.dev
MIT License
1.03k stars 32 forks source link

node-rs/bcrypt: RollupError: Unexpected character '\u{7f}' #816

Open ZejusJS opened 3 months ago

ZejusJS commented 3 months ago

I'm having issue during building a SvelteKit project with napi-rs/node-rs. The issue occurs when building it in Docker image but when I build it on Vercel it's working.

I tried to use vite.config.ts from use-napi-rs-packages-with-bundlers but I'm only getting another error (SvelteKit can't use webpack).

✓ built in 15.37s

> Using @sveltejs/adapter-node
error during build:
RollupError: Unexpected character '\u{7f}'
     at getRollupError (file:///app/node_modules/.pnpm/rollup@4.13.2/node_modules/rollup/dist/es/shared/parseAst.js:376:41)
     at ParseError.initialise (file:///app/node_modules/.pnpm/rollup@4.13.2/node_modules/rollup/dist/es/shared/node-entry.js:11175:28)
     at convertNode (file:///app/node_modules/.pnpm/rollup@4.13.2/node_modules/rollup/dist/es/shared/node-entry.js:12915:10)
     at convertProgram (file:///app/node_modules/.pnpm/rollup@4.13.2/node_modules/rollup/dist/es/shared/node-entry.js:12235:12)
     at Module.setSource (file:///app/node_modules/.pnpm/rollup@4.13.2/node_modules/rollup/dist/es/shared/node-entry.js:14074:24)
     at async ModuleLoader.addModuleSource (file:///app/node_modules/.pnpm/rollup@4.13.2/node_modules/rollup/dist/es/shared/node-entry.js:18713:13)
------
Dockerfile:12
--------------------
  10 |     ENV NODE_ENV=production
  11 |     ENV IS_DOCKER=true
  12 | >>> RUN npm run build
  13 |
  14 |     FROM node:18.20.0-alpine3.19
--------------------
ERROR: failed to solve: process "/bin/sh -c npm run build" did not complete successfully: exit code: 1

I'm using @sveltejs/kit@^2.5.5, @node-rs/bcrypt@^1.10.1 and vite@^5.2.7. This error occurs in another dockerized SvelteKit projects aswell.

"devDependencies": {
    "@internationalized/date": "^3.5.2",
    "@melt-ui/pp": "^0.3.0",
    "@melt-ui/svelte": "^0.74.4",
    "@node-rs/bcrypt": "^1.10.1",
    "@prgm/sveltekit-progress-bar": "^2.0.0",
    "@sveltejs/adapter-auto": "^3.2.0",
    "@sveltejs/adapter-node": "^5.0.1",
    "@sveltejs/adapter-vercel": "^5.2.0",
    "@sveltejs/kit": "^2.5.5",
    "@sveltejs/vite-plugin-svelte": "^3.0.2",
    "@tanstack/query-persist-client-core": "^5.28.9",
    "@tanstack/query-sync-storage-persister": "^5.28.9",
    "@tanstack/react-query-persist-client": "^5.28.9",
    "@tanstack/svelte-query": "^5.28.9",
    "@tanstack/svelte-query-devtools": "^5.28.10",
    "@typegoose/auto-increment": "^4.2.0",
    "@types/bcrypt": "^5.0.2",
    "@types/crypto-js": "^4.2.2",
    "@types/js-cookie": "^3.0.6",
    "@types/mongoose-auto-increment": "^5.0.38",
    "@types/mongoose-sequence": "^3.0.11",
    "@types/uuid": "^9.0.8",
    "@vitejs/plugin-react": "^4.2.1",
    "axios": "^1.6.8",
    "crypto-js": "^4.2.0",
    "jose": "^5.2.3",
    "js-cookie": "^3.0.5",
    "mongoose": "^8.2.4",
    "mongoose-auto-increment": "^5.0.1",
    "mongoose-paginate-v2": "^1.8.0",
    "mongoose-sequence": "^6.0.1",
    "rollup-plugin-typescript2": "^0.36.0",
    "sass": "^1.72.0",
    "svelte": "^4.2.12",
    "svelte-check": "^3.6.8",
    "swrv": "^1.0.4",
    "ts-node": "^10.9.2",
    "ts-patch": "^3.1.2",
    "tslib": "^2.6.2",
    "typescript": "^5.4.3",
    "uuid": "^9.0.1",
    "vite": "^5.2.7"
},

My Dockerfile is:

FROM node:18.20.0-alpine3.19 AS builder

RUN npm install -g pnpm

WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN pnpm i

COPY . .
ENV NODE_ENV=production
ENV IS_DOCKER=true
RUN npm run build

FROM node:18.20.0-alpine3.19
WORKDIR /app
COPY --from=builder /app/build build/
COPY package.json .
EXPOSE 3000
CMD ["node", "build"]
squeezeboxhuf commented 2 months ago

I am having the same problem. I have a sveltekit application, runs fine in dev or preview mode, but the build process fails with the error as above :-

error during build:RollupError:
 Unexpected character '\u{7f}'

which I tracked down to being in the Argon2 module argon2-linux-arm64-gnu module that my Lucia auth packages bring in. I get the same on 2 different runtime platforms - Arm and Intel.

Brooooooklyn commented 2 months ago

See https://github.com/napi-rs/use-napi-rs-packages-with-bundlers

ZejusJS commented 2 months ago

See https://github.com/napi-rs/use-napi-rs-packages-with-bundlers

Still can't get it to work.

When i use the Vite example, I get another error:

ReferenceError [Error]: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.        
at file:///app/.svelte-kit/output/server/entries/endpoints/api/_server.ts.js:5:29
...

It compiles everyting. So I tried to use only ViteNodeAddonPlugin function, but I got the same error RollupError: Unexpected character '\u{7f}'.

paulwongx commented 1 month ago

Same issue. Bump.

vhochstein commented 1 month ago

Looks like I am facing same issue. sveltekit and Lucia. dev build is working prod build fails with same error message. Ive tried node 18 and 20. Both same error I ve tried macos and ubuntu both same error.

I am totally blocked by this and I ve got no idea how to solve this :-( If anybody has a hint, would be grately appreciated

/Users/volkerhochstein/projects/node/magic-pull/node_modules/@node-rs/argon2-darwin-arm64/argon2.darwin-arm64.node error during build: RollupError: Unexpected character '�' at getRollupError (file:///Users/volkerhochstein/projects/node/magic-pull/node_modules/rollup/dist/es/shared/parseAst.js:395:41)

"@lucia-auth/adapter-prisma": "^4.0.1",
"@prisma/client": "^5.13.0",
"@sveltejs/adapter-node": "^5.0.1",
"lucia": "^3.2.0",
"oslo": "^1.2.0",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"vite-plugin-tailwind-purgecss": "0.3.3",
"@sveltejs/kit": "^2.5.7",
"@sveltejs/vite-plugin-svelte": "^3.1.0",
squeezeboxhuf commented 1 month ago

I've managed to get my build working, but to do it I stopped using the oslo package. I was using Argon2 from oslo to create password hashes, and I thought that was where the problem lay so I switched to bcrypt. I think what made the main difference though, was moving the bcrypt package (and my better-sqlite3 package) out of devDependcies into dependencies in my package.json :

  "devDependencies": {
    "@lucia-auth/adapter-sqlite": "^3.0.1",
    "@skeletonlabs/skeleton": "^2.9.0",
    "@skeletonlabs/tw-plugin": "^0.3.1",
    "@sveltejs/adapter-auto": "^3.0.0",
    "@sveltejs/adapter-node": "^5.0.1",
    "@sveltejs/kit": "^2.0.0",
    "@sveltejs/vite-plugin-svelte": "^3.0.0",
    "@tailwindcss/forms": "^0.5.7",
    "@types/better-sqlite3": "^7.6.9",
    "@types/d3": "^7.4.3",
    "@types/eslint": "^8.56.0",
    "@types/leaflet": "^1.9.12",
    "@typescript-eslint/eslint-plugin": "^7.0.0",
    "@typescript-eslint/parser": "^7.0.0",
    "autoprefixer": "^10.4.16",
    "d3": "^7.9.0",
    "eslint": "^8.56.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-svelte": "^2.35.1",
    "express": "^4.19.2",
    "iconify-icon": "^2.0.0",
    "lucia": "^3.1.1",
    "postcss": "^8.4.32",
    "postcss-load-config": "^5.0.2",
    "prettier": "^3.1.1",
    "prettier-plugin-svelte": "^3.1.2",
    "prettier-plugin-tailwindcss": "^0.5.9",
    "svelte": "^4.2.7",
    "svelte-check": "^3.6.0",
    "tailwindcss": "^3.4.3",
    "tslib": "^2.4.1",
    "typescript": "^5.4.5",
    "vite": "^5.0.3"
  },
  "type": "module",
  "dependencies": {
    "@types/bcrypt": "^5.0.2",
    "bcrypt": "^5.1.1",
    "better-sqlite3": "^9.6.0",
    "dotenv": "^16.4.5",
    "leaflet": "^1.9.4"
  }
dBianchii commented 3 hours ago
│   ▲ Next.js 14.2.4
│ 
│    Creating an optimized production build ...
│ Failed to compile.
│ 
│ ../../packages/api/node_modules/@node-rs/argon2-linux-x64-gnu/argon2.linux-x64-gnu.node
│ Module parse failed: Unexpected character '' (1:0)
│ You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders