-
Notifications
You must be signed in to change notification settings - Fork 10
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
leverage NodeJS resolve logic for looking up location of paths for node modules #557
Comments
As part of the WCCG, starting a repo for experimenting with this. |
Will likely want to do an
|
Cool, the |
Observed as part of #716 that were still some critical hard coded paths assuming root level location of _node_modules, so will be re-opening this to try and clean those instances up. |
Hmm, found a regression in a Lit@2 repo where Unable to look up package using NodeJS require.resolve for => @types/trusted-types Error: Cannot find module '@types/trusted-types'
Require stack:
- /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/lib/node-modules-utils.js
- /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/plugins/resource/plugin-node-modules.js
- /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/lifecycles/config.js
- /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/lifecycles/compile.js
- /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/commands/build.js
- /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.resolve (internal/modules/cjs/helpers.js:94:19)
at getNodeModulesResolveLocationForPackageName (/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/lib/node-modules-utils.js:7:42)
at getPackageEntryPath (/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/plugins/resource/plugin-node-modules.js:36:49)
at /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/plugins/resource/plugin-node-modules.js:102:19
at Array.forEach (<anonymous>)
at walkPackageJson (/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/plugins/resource/plugin-node-modules.js:98:47)
at /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/plugins/resource/plugin-node-modules.js:186:9
at Array.forEach (<anonymous>)
at walkPackageJson (/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/plugins/resource/plugin-node-modules.js:98:47) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/lib/node-modules-utils.js',
'/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/plugins/resource/plugin-node-modules.js',
'/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/lifecycles/config.js',
'/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/lifecycles/compile.js',
'/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/commands/build.js',
'/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@greenwood/cli/src/index.js'
]
} it is a dependency of lit-html % yarn why @types/trusted-types
yarn why v1.22.5
[1/4] 🤔 Why do we have the module "@types/trusted-types"...?
[2/4] 🚚 Initialising dependency graph...
[3/4] 🔍 Finding dependency...
[4/4] 🚡 Calculating file sizes...
=> Found "@types/[email protected]"
info Reasons this module exists
- "lit#lit-html" depends on it
- Hoisted from "lit#lit-html#@types#trusted-types"
info Disk size without dependencies: "16KB"
info Disk size with unique dependencies: "16KB"
info Disk size with transitive dependencies: "16KB"
info Number of shared dependencies: 0
✨ Done in 0.54s. Will try and narrow down why this is an issue now, but seems related to #725 |
Found found the root cause? It seems that packages with no main field or an empty main field bomb out on usage of // index,js
require.resolve('@babel/runtime') node index.js
LIT 111 /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/lit-html/lit-html.js
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/node_modules/@babel/runtime/package.json
at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)
at packageExportsResolve (internal/modules/esm/resolve.js:513:3)
at resolveExports (internal/modules/cjs/loader.js:432:36)
at Function.Module._findPath (internal/modules/cjs/loader.js:472:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27)
at Function.resolve (internal/modules/cjs/helpers.js:94:19)
at Object.<anonymous> (/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/index.js:3:39)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
} // index,js
require.resolve('@types/trusted-types'); % node index.js
Error: Cannot find module '@types/trusted-types'
Require stack:
- /Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.resolve (internal/modules/cjs/helpers.js:94:19)
at Object.<anonymous> (/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/index.js:4:39)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/owenbuckley/Workspace/contributary/repos/www.contributary.community/index.js'
]
} Maybe this is something that will be accounted for as part of ESM migration, by honoring export maps at least - #707 |
Type of Change
Summary
As part of looking into #505 as part of submitting #511 and so for then, was just getting something done that was quick and dirty, using unpkg.com as a fallback. I'm not sure if this will support everything like things added to
importMap
... but I did reach out in NodeJS slack channel and got this tip on using NodeJSrequire.resolve
and so that would certainly be interesting because if we could piggy back somehow of off Node's default resolve behavior, that would allow us to maintain all that on our own.This would also ensure that when Greenwood is run from the command line using
npx
, node_modules would always resolve correctly.Details
So essentially, give it a package name and it will look up the path for you.
With a better local testing environment (using
npm|yarn link
), we could set it up so that it in plugins-node-modulesresolve
just delegates all this torequire.resolve
instead:We might want to generate our own full path since the
url
is what we want based on walking package.json (in theory) as opposed to what NodeJS thinks we want.Given that, perhaps there is a case for us to entirely refactor
getPackageEntryPath
to be based offrequire.resolve
logic instead? 🤔We intentionally always want to favor ESM if we can, so I guess it depends on if anything would break but we certainly have enough tests for that. 😃
Another consideration might be around when to specify extensions being added to files, like the browser does, as currently right now for anything from node_modules it is not required, but any components or JS in your project do. Maybe a case for a plugin that will handle resolving extensions a la webpack?
The text was updated successfully, but these errors were encountered: