nitro icon indicating copy to clipboard operation
nitro copied to clipboard

[Nitro/Unenv] cloudflare_pages build error: Could not resolve import string_decoder

Open andrevandal opened this issue 2 years ago • 36 comments

Environment

  • node: 18.16.1
  • nuxt: 3.7.4
  • drizzle-kit: 0.19.13
  • @nuxtjs/tailwindcss: 6.8.0
  • @planetscale/database: 1.11.0
  • drizzle-orm: 0.28.6
  • pnpm: 8.9.0

Reproduction

Clone the repo: https://github.com/andrevandal/ondevamos-web/tree/start-backend Build.

I tried to build it through pnpm and npm, but both got errors.

Describe the bug

When building the nuxt project, we got the error:

ℹ Building Nitro Server (preset: cloudflare_pages)                           nitro 2:18:00 PM
(node-resolve plugin) Could not resolve import "unenv/runtime/node/string_decoder/index/" in /Users/andre.vandal/projects/github.com/andrevandal/ondevamos-web/node_modules/readable-stream/lib/_stream_readable.js using exports defined in /Users/andre.vandal/projects/github.com/andrevandal/ondevamos-web/node_modules/unenv/package.json.

[nitro 2:18:03 PM]  ERROR  Error: Cannot resolve "unenv/runtime/node/string_decoder/index/" from "/Users/andre.vandal/projects/github.com/andrevandal/ondevamos-web/node_modules/readable-stream/lib/_stream_readable.js" and externals are not allowed!


undefined


[2:18:03 PM]  ERROR  Cannot resolve "unenv/runtime/node/string_decoder/index/" from "/Users/andre.vandal/projects/github.com/andrevandal/ondevamos-web/node_modules/readable-stream/lib/_stream_readable.js" and externals are not allowed!

  at Object.resolveId (node_modules/nuxt/node_modules/nitropack/dist/shared/nitro.0093b44c.mjs:1879:17)
  at async PluginDriver.hookFirstAndGetPlugin (node_modules/rollup/dist/es/shared/node-entry.js:25444:28)
  at async resolveId (node_modules/rollup/dist/es/shared/node-entry.js:24117:26)
  at async ModuleLoader.resolveId (node_modules/rollup/dist/es/shared/node-entry.js:24531:15)
  at async PluginDriver.hookFirstAndGetPlugin (node_modules/rollup/dist/es/shared/node-entry.js:25444:28)
  at async resolveId (node_modules/rollup/dist/es/shared/node-entry.js:24117:26)
  at async ModuleLoader.resolveId (node_modules/rollup/dist/es/shared/node-entry.js:24531:15)
  at async node_modules/@rollup/plugin-commonjs/dist/es/index.js:781:16
  at async Promise.all (index 10)
  at async node_modules/@rollup/plugin-commonjs/dist/es/index.js:773:32

Additional context

Related issues:

  • https://github.com/unjs/unenv/issues/129
  • https://github.com/nuxt/nuxt/issues/21619

Logs

No response

andrevandal avatar Oct 11 '23 17:10 andrevandal

I've got the same error with "vercel-edge" Preset:

ℹ Building Nitro Server (preset: vercel-edge)                                    nitro 21:32:11
(node-resolve plugin) Could not resolve import "unenv/runtime/node/string_decoder/index/" in D:\BisschenProggen\shopifyInspector\node_modules\lambdafs\node_modules\tar-stream\node_modules\readable-stream\lib\_stream_readable.js using exports defined in D:\BisschenProggen\shopifyInspector\node_modules\unenv\package.json.

[nitro 21:32:13]  ERROR  Error: Cannot resolve "unenv/runtime/node/string_decoder/index/" from "D:\\BisschenProggen\\shopifyInspector\\node_modules\\lambdafs\\node_modules\\tar-stream\\node_modules\\readable-stream\\lib\\_stream_readable.js" and externals are not allowed!


undefined


[21:32:13]  ERROR  Cannot resolve "unenv/runtime/node/string_decoder/index/" from "D:\\BisschenProggen\\shopifyInspector\\node_modules\\lambdafs\\node_modules\\tar-stream\\node_modules\\readable-stream\\lib\\_stream_readable.js" and externals are not allowed!

  at Object.resolveId (/D:/BisschenProggen/shopifyInspector/node_modules/nitropack/dist/shared/nitro.0093b44c.mjs:1879:17)
  at async PluginDriver.hookFirstAndGetPlugin (/D:/BisschenProggen/shopifyInspector/node_modules/rollup/dist/es/shared/node-entry.js:25444:28)
  at async resolveId (/D:/BisschenProggen/shopifyInspector/node_modules/rollup/dist/es/shared/node-entry.js:24117:26)
  at async ModuleLoader.resolveId (/D:/BisschenProggen/shopifyInspector/node_modules/rollup/dist/es/shared/node-entry.js:24531:15)
  at async PluginDriver.hookFirstAndGetPlugin (/D:/BisschenProggen/shopifyInspector/node_modules/rollup/dist/es/shared/node-entry.js:25444:28)
  at async resolveId (/D:/BisschenProggen/shopifyInspector/node_modules/rollup/dist/es/shared/node-entry.js:24117:26)
  at async ModuleLoader.resolveId (/D:/BisschenProggen/shopifyInspector/node_modules/rollup/dist/es/shared/node-entry.js:24531:15)
  at async /D:/BisschenProggen/shopifyInspector/node_modules/@rollup/plugin-commonjs/dist/es/index.js:781:16
  at async Promise.all (index 10)
  at async /D:/BisschenProggen/shopifyInspector/node_modules/@rollup/plugin-commonjs/dist/es/index.js:773:32



[21:32:13]  ERROR  Cannot resolve "unenv/runtime/node/string_decoder/index/" from "D:\\BisschenProggen\\shopifyInspector\\node_modules\\lambdafs\\node_modules\\tar-stream\\node_modules\\readable-stream\\lib\\_stream_readable.js" and externals are not allowed!

This is my package.json file:

{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@nuxt/devtools": "latest",
    "@nuxtjs/tailwindcss": "^6.8.0",
    "nuxt": "^3.7.4",
    "unenv": "^1.7.4",
    "vue": "^3.3.4",
    "vue-router": "^4.2.5"
  },
  "dependencies": {
    "chrome-aws-lambda": "^10.1.0",
    "puppeteer": "^10.1.0",
    "puppeteer-core": "^10.4.0"
  }
}

Building on

Building Nitro Server (preset: node-server)   

is fine without any problems

DHartenfels avatar Oct 12 '23 19:10 DHartenfels

Same but a different module:

[error] [nitro] Error: Cannot resolve "bufferutil" from "/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/ws/lib/buffer-util.js" and externals are not allowed!

Gruce avatar Oct 20 '23 23:10 Gruce

Having the same issue

[error] Cannot resolve "unenv/runtime/node/string_decoder/index/" from "/opt/buildhome/repo/node_modules/imap/node_modules/readable-stream/lib/_stream_readable.js" and externals are not allowed!

vptill avatar Oct 31 '23 14:10 vptill

Any solution to this? I am currently on firebase preset (with no error) and want to try cloudflare. Changing the preset to cloudflare-pages gives this error.

ManasMadrecha avatar Nov 03 '23 12:11 ManasMadrecha

Currently trying to build via the deployment guide for cloudflare and getting this exact same error. Any updates on this?

aw-0 avatar Dec 16 '23 00:12 aw-0

Same but a different module:

[error] [nitro] Error: Cannot resolve "bufferutil" from "/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/ws/lib/buffer-util.js" and externals are not allowed!

try yarn add bufferutil utf-8-validate. it worked for me

megatheikal avatar Jan 10 '24 01:01 megatheikal

Same but a different module:

[error] [nitro] Error: Cannot resolve "bufferutil" from "/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/ws/lib/buffer-util.js" and externals are not allowed!

try yarn add bufferutil utf-8-validate. it worked for me

This workaround confirmed.

But it may cause another bug on cloudflare: image

Thank you.

Gruce avatar Jan 10 '24 11:01 Gruce

In my nuxt.config.ts file I have this, if it helps

    rollupConfig: {
      external: ["bufferutil", "utf-8-validate"],
      output: {
        generatedCode: {
          symbols: true,
        },
      },
    },

zakarialounes avatar Jan 11 '24 02:01 zakarialounes

In my nuxt.config.ts file I have this, if it helps

    rollupConfig: {
      external: ["bufferutil", "utf-8-validate"],
      output: {
        generatedCode: {
          symbols: true,
        },
      },
    },

Same problem, nothing changed.

HassanKadhim avatar Jan 12 '24 18:01 HassanKadhim

the problem is that unenv ins't converting JavaScript code to Cloudflare compatible code.

adding modules to external (https://github.com/unjs/nitro/issues/1821#issuecomment-1884060776) just says to rollup this module will be available in runtime, which isn't (https://github.com/unjs/nitro/issues/1821#issuecomment-1884683426).

this is the map table of how unenv convert some modules: https://github.com/unjs/unenv?tab=readme-ov-file#built-in-nodejs-modules

andrevandal avatar Jan 16 '24 15:01 andrevandal

Was a solution ever found to @andrevandal's problem? It seems like him and many others are experiencing the same issue with no luck.

elliotjarnit avatar Jan 30 '24 15:01 elliotjarnit

Same issue here unfortunately.

devbrains-com avatar Jan 30 '24 20:01 devbrains-com

Same issue here, if anyone fix this, @ me pls

larify avatar Feb 02 '24 03:02 larify

Hi. We are aware and this issue is under the track.

The issue is that you are using a library that is designed for Node.js and when trying to deploy to Cloudflare pages, Nitro (while trying to fix this with unenv) is not covering some subpath compatibility.

I should spend more time investigating what exact library is incompatible and a minimal (nitro only with only problematic dependency) could help to speed this up.

pi0 avatar Feb 02 '24 09:02 pi0

I got the exact same issue when importing firebase-admin inside a nitro plugin. But I figured out that it was the import that was incorrect. A minimal reproduction can be found at https://stackblitz.com/edit/github-znntty

The incorrect version was

import admin from 'firebase-admin';

export default defineNitroPlugin((nitro) => {
  const test = admin.initializeApp;
});

The errors disappeared after correcting the code to the following.

import { initializeApp } from 'firebase-admin/app';

export default defineNitroPlugin((nitro) => {
  const test = initializeApp;
});

milindanath avatar Feb 04 '24 16:02 milindanath

I got the exact same issue when importing firebase-admin inside a nitro plugin. But I figured out that it was the import that was incorrect. A minimal reproduction can be found at https://stackblitz.com/edit/github-znntty

The incorrect version was

import admin from 'firebase-admin';

export default defineNitroPlugin((nitro) => {
  const test = admin.initializeApp;
});

The errors disappeared after correcting the code to the following.

import { initializeApp } from 'firebase-admin/app';

export default defineNitroPlugin((nitro) => {
  const test = initializeApp;
});

I made this change but it didn't work on my end, I'm still getting the same error "Cannot resolve "unenv/runtime/node/string_decoder/index/""

kylebuxton avatar Feb 04 '24 19:02 kylebuxton

after some research the problem seems to be with import {getFirestore} from "firebase-admin/firestore"; as when I remove this import, building works perfectly.

I don't know why this could be but I will keep looking.

elliotjarnit avatar Feb 20 '24 21:02 elliotjarnit

Can add the following library also to the issue list [nitro] Error: Cannot resolve "aws-sdk" from "/opt/atlassian/pipelines/agent/build/node_modules/@opensearch-project/opensearch/lib/aws/AwsSigv4Signer.js" and externals are not allowed!

AmirMechouk avatar Mar 12 '24 18:03 AmirMechouk

Same with KnexJS

[error] [nitro] Error: Cannot resolve "pg" from "/opt/buildhome/repo/node_modules/knex/lib/dialects/redshift/index.js" and externals are not allowed!

Mgoeloe avatar Apr 12 '24 00:04 Mgoeloe