Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subpath imports don't work for built-in Node modules #310

Closed
nex3 opened this issue Sep 28, 2021 · 7 comments
Closed

Subpath imports don't work for built-in Node modules #310

nex3 opened this issue Sep 28, 2021 · 7 comments

Comments

@nex3
Copy link

nex3 commented Sep 28, 2021

Support for subpath imports was added in #293, but they're broken if one of the subpaths is a built-in Node.js module. To reproduce:

mkdir -p test/lib
cd test
npm init -f
npm i webpack webpack-cli

package.json:

{
    "name": "webpack-imports",
    "version": "1.0.0",
    "type": "module",
    "imports": {
        "#fs": {"node": "fs"}
    },
    "devDependencies": {
        "webpack": "^5.36.0",
        "webpack-cli": "^4.6.0"
    }
}

lib/main.js:

import '#fs';

Run npx webpack build --target node --entry ./lib/main.js. It produces this error:

assets by status 123 bytes [cached] 1 asset
./lib/main.js 14 bytes [built] [code generated]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

ERROR in ./lib/main.js 1:0-13
Module not found: Error: Can't resolve '#fs' in '/tmp/test/lib'
resolve '#fs' in '/tmp/test/lib'
  using description file: /tmp/test/package.json (relative path: ./lib)
    resolve as internal import
      using imports field: fs
        Parsed request is a module
        using description file: /tmp/test/package.json (relative path: ./lib)
          resolve as module
            /tmp/test/lib/node_modules doesn't exist or is not a directory
            looking for modules in /tmp/test/node_modules
              single file module
                using description file: /tmp/test/package.json (relative path: ./node_modules/fs)
                  no extension
                    /tmp/test/node_modules/fs doesn't exist
                  .js
                    /tmp/test/node_modules/fs.js doesn't exist
                  .json
                    /tmp/test/node_modules/fs.json doesn't exist
                  .wasm
                    /tmp/test/node_modules/fs.wasm doesn't exist
              /tmp/test/node_modules/fs doesn't exist
            /tmp/node_modules doesn't exist or is not a directory
            /node_modules doesn't exist or is not a directory

webpack 5.54.0 compiled with 1 error and 1 warning in 168 ms
@vankop
Copy link
Member

vankop commented Sep 28, 2021

As a hack for now I could suggest to put ./fs.js in your app, where fs.js

module.exports = require('fs');

@sokra
Copy link
Member

sokra commented Sep 30, 2021

The workaround by @vankop sounds good. This is a little bit complicated to fix...

@nex3
Copy link
Author

nex3 commented Sep 30, 2021

Does it? For non-subpath imports, Webpack already knows to avoid trying to bundle require('fs'). I'd assume that it could just re-use the same logic here.

@vankop
Copy link
Member

vankop commented Sep 30, 2021

@nex3 I have created small example, take a look https://github.com/vankop/bundle-fs

@alexander-akait
Copy link
Member

Let's close in favor of #303

@nex3
Copy link
Author

nex3 commented Jul 23, 2024

I don't think #303 addresses this—the core issue here is that this behavior is specified to work in Node.js, but Webpack is incompatible with it. In other words, this is a bug report, not a feature request that could be addressed by supporting the feature in some other way.

@alexander-akait
Copy link
Member

It is the same, we don't support resolution built-in Node.js modules neither in imports nor in normal request, and we have never supported this here, so this is an improvement request for us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants