-
Notifications
You must be signed in to change notification settings - Fork 71
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
Bad export directive since 42f54e8010c077c5375f0c96ab35a12bad35d3f4 (Jan 23) #142
Comments
Not sure what you mean :) The import is working fine: https://stackblitz.com/edit/node-3ejlnp?file=package.json,index.js Maybe you need to switch to ECMAScript modules to make it work. See #146 (comment) |
Thanks @jhnns, #146 comment was useful. src/index.ts:
|
Yes, that's basically it, though I'd rewrite that example to const parseDomainModule = import('parse-domain');
async function parseDomain(url) {
const { parseDomain, fromUrl } = await parseDomainModule;
return parseDomain(fromUrl(url));
}
parseDomain('https://www.münchen.de?query').then((result) => {
console.log(result);
}); https://stackblitz.com/edit/node-q4wvvq?file=package.json,index.js |
Alternatively, you could write a helper function that initializes all ESM dependencies before starting the application: // the helper
const modules = {
['parse-domain']: import('parse-domain'),
};
const initializedModules = Object.create(null);
exports.require = (moduleName) => {
if (moduleName in initializedModules === false) {
throw new Error(`Unknown module ${moduleName}`);
}
return initializedModules[moduleName];
};
exports.isReady = (async () => {
for (const [moduleName, modulePromise] of Object.entries(modules)) {
initializedModules[moduleName] = await modulePromise;
}
})(); // on application start
const esm = require('./esm.js');
esm.isReady.then(() => {
require('./app.js');
}); // in your app
const esm = require('./esm.js');
const { parseDomain, fromUrl } = esm.require('parse-domain');
console.log(parseDomain(fromUrl('www.münchen.de'))); https://stackblitz.com/edit/node-whxrib?file=esm.js,start.js,app.js |
Hello,
Thanks for your very useful code.
in src/main.ts
from './parse-domain'
becomes
from './parse-domain.js'
which throws an error when we are using your module.
Same for
from './sanitize.js'
Thanks,
Frédéric
The text was updated successfully, but these errors were encountered: