niksy / node-stdlib-browser

Node standard library for browser.
MIT License
189 stars 13 forks source link

Unable to import in REPL #35

Closed Richienb closed 1 week ago

Richienb commented 1 week ago

Node.js REPL:

Welcome to Node.js v20.15.1.
Type ".help" for more information.
> require('node-stdlib-browser')
Uncaught Error: Cannot find module './mock/empty.js'
Require stack:
- <repl>
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Function.resolve (node:internal/modules/helpers:190:19)
    at resolvePath (C:\Users\richi\Documents\GitHub\node-polyfill-webpack-plugin\node_modules\node-stdlib-browser\cjs\index.js:37:333) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<repl>' ]
}
> await import('node-stdlib-browser')
Uncaught Error: Cannot find module './mock/empty.js'
Require stack:
- <repl>
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Function.resolve (node:internal/modules/helpers:190:19)
    at resolvePath (file:///C:/Users/richi/Documents/GitHub/node-polyfill-webpack-plugin/node_modules/node-stdlib-browser/esm/index.js:30:133)
    at file:///C:/Users/richi/Documents/GitHub/node-polyfill-webpack-plugin/node_modules/node-stdlib-browser/esm/index.js:40:23
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:432:15)
    at async REPL2:1:33 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<repl>' ]
}

./mock/empty.js does exist though, maybe the relative resolution system broke somehow.

Able to import from a regular file though.

niksy commented 1 week ago

It seems like issue is here: https://github.com/niksy/node-stdlib-browser/blob/master/index.js#L9

Adding initial check for require before globalThis.require seemingly uses different globally available method. I thought these are the same methods but this doesn’t seem to be the case.

There’s even module check script for checking CJS and ESM format which passes every time, but this really seems like weird edge case.