Update: Some interesting (interesting as in confusing) data here. From within my react component’s render()
method, this works:
import('npm-module')
.then(r => console.log('Imported', r))
.catch(error => console.log('Caught Error from import:', error.stack));
This doesn’t (wrapping the import call in a closure):
const doimport = p => import(p);
doimport('npm-module')
.then(r => console.log('Imported', r))
.catch(error => console.log('Caught Error from import:', error.stack));
And neither does this (providing a const string to import):
const name = 'npm-module';
import(name)
.then(r => console.log('Imported', r))
.catch(error => console.log('Caught Error from import:', error.stack));
So the ONLY way it works for me is if you statically provide the npm-module name to import (e.g. the name is literally present at compile-time without any variable substitution etc). I’ve also tracked down at least a next step as to why they fail.
It comes down to modules-runtime.fileAppendId
(NOTE: looking at the source of fileAppendId would be helpful here):
-
When import()
works, ‘npm-module’ is present in the file.content of node_modules. That is fileAppendId(File[node_modules], ‘npm-module’, [extensions array]) returns a valid object corresponding to ‘npm-module’, as it should to import/require the package.
-
When import()
does not work, ‘npm-module’ is not present in the file.content of node_modules. Thus, the client has no idea this package exists and it can never be resolved.
I know this is probably not shocking (a missing file can’t be resolved, right?), but it’s verification that the import/require process is itself working as it should, but that the ‘npm-module’ is completely missing (from the client’s point of view) in failure cases.
Next up: to find out where that module gets lost.
UPDATE:
In working cases, web.browser/dynamic/node_modules/
contains ‘npm-modules’ and in non-working cases, the npm module is missing. So…
Throwing the ball back to @robfallows or anyone else who wants to chime in. How can I get ‘npm-module’ to stay in my dynamic/node_modules
folder if it’s not imported literally?
Am I going to have to have a file that includes a bunch of import(‘npm-module’) statements and never reference the file? Nope. That doesn’t work.