sindresorhus / globby

User-friendly glob matching
MIT License
2.51k stars 130 forks source link

Failed to resolve import "node:fs" #184

Closed flayks closed 3 years ago

flayks commented 3 years ago

I'm using Globby to find all routes on my SvelteKit site and since the upgrade to version 12, it throws me this error:

Failed to resolve import "node:fs" from "node_modules/.pnpm/globby@12.0.0/node_modules/globby/index.js". Does the file exist?
1  |  import fs from 'node:fs';
   |                  ^
2  |  import arrayUnion from 'array-union';
3  |  import merge2 from 'merge2';

SvelteKit is using Vite, and I have Node v14.17.4 installed (latest stable)

My code:

import { globby } from 'globby'

export async function get ({ headers, path, query, params, host }) {
    // Get pages from Svelte routes
    const pagesFiles = await globby([
        `./src/routes/*.svelte`, // Get all svelte routes
        `!./src/routes/{__*,[*,index}.svelte`, // Svelte related pages
        `!./src/routes/{project,api}`, // Directories
    ])

I've never seen the node:fs syntax for importing, is that new or is it a typo?

a-b-r-o-w-n commented 3 years ago

This is a breaking change for node versions < 16 when using require, we just downgraded to an earlier version.

PabloSzx commented 3 years ago

@sindresorhus can this please be fixed? because of this the Require Node.js 12.20 is not even true 😞

Zomono commented 3 years ago

I'm currently on electron 13 (latest) which is based on node 14.17 having the same error. Electrons next version may also be based on node 14, so upgrading is not an option. Hope for a fix.

sindresorhus commented 3 years ago

SvelteKit is using Vite, and I have Node v14.17.4 installed (latest stable)

This is a problem with Vite and you should open an issue there instead.

sindresorhus commented 3 years ago

This is a breaking change for node versions < 16 when using require, we just downgraded to an earlier version.

Per the release notes, require is no longer supported.

sindresorhus commented 3 years ago

I'm currently on electron 13 (latest) which is based on node 14.17 having the same error. Electrons next version may also be based on node 14, so upgrading is not an option. Hope for a fix.

You need to open an issue on Electron. This package supports Node.js. When used with Electron, you're on your own.

flayks commented 3 years ago

SvelteKit is using Vite, and I have Node v14.17.4 installed (latest stable)

This is a problem with Vite and you should open an issue there instead.

Most libraries work with Vite (which is based on esbuild btw), even importing with import, so I think comes to globby changing how it works from v11 to v12?

Trinovantes commented 2 years ago

I'm working in a JS env that does not support ESM (GitHub actions only support Node 12 CJS [1]). My problem isn't that require is no longer supported -- webpack handles the conversion just fine -- but that node:fs is used instead of fs (https://github.com/sindresorhus/globby/blob/main/index.js#L1). This path format is invalid in CJS below Node 16 even after being processed by webpack. It's a bit silly to have to sed -i s/node://g after running webpack.

[1] Technically GH Actions can support ESM by writing my own Dockerfile but it requires an excessive amount of boilerplate code to hook inputs/outputs with GH's runtime

DavidAnson commented 2 years ago

If you're trying to use webpack to bundle globby 12.0.2 for use from a CommonJS module, the following configuration may help with the broken "node:" imports (cc: @Trinovantes):

    "externals": {
        "node:fs": "commonjs fs",
        "node:path": "commonjs path",
        "node:stream": "commonjs stream",
        "node:util": "commonjs util"
    }