
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
esm-loader-import-relative-add-extension
Advanced tools
Chainable ESModule Loader to add missing file extensions to relative imports
Instead, you can find the same functionality, plus additional features, here: esm-loader-import-relative-extension
Node.js ESModule Loader for adding missing file extensions to relative imports, allowing Node.js to actually resolve the file.
You can use this for .js
files, or Node has a built-in flag
--experimental-specifier-resolution=node
which may work instead. For any
file extensions other than .js
, this is the loader for you.
Warning! Using experimental Node.js features and flags, API will likely change. This may be helpful for development and testing, but should not be used in production.
For example, when using Typescript, the imports of other Typescript files are usually suggested to NOT use any file extension:
// lib.ts
const result: Record<string, string> = {
welcome: 'hello',
}
export default result.welcome
// index.ts
import welcome from './lib' // NO EXT!
console.dir(welcome) // 'hello'
However, if we are using loaders with Node.js, this will fail as Node has no idea what to do with this. Using our loader, we'll add wanted file extensions to these imports on-the-fly, allowing Node to resolve the file after all.
This loader can be configured, and chained with other loaders, using node-esm-loader.
npm install --save-dev \
esm-loader-import-relative-add-extension \
esm-loader-typescript \
node-esm-loader
// .loaderrc.js
export default {
loaders: [
'esm-loader-typescript',
{
loader: 'esm-loader-import-relative-add-extension',
options: {
extensions: {
'.ts': '.ts',
},
},
},
],
}
// .loaderrc.js
export default {
loaders: [
{
loader: 'esm-loader-import-relative-add-extension',
options: {
extensions: {
// Any relative imports found in .js files, which are missing a file
// extension, will have the '.js' extension added on-the-fly.
'.js': '.js',
// Any relative imports found in .ts or .svelte files, which are
// missing a file extension, will have the '.ts' extension added
// on-the-fly.
'.ts': '.ts',
'.svelte': '.ts',
},
},
},
],
}
// .loaderrc.js
export default {
loaders: [
{
loader: 'esm-loader-import-relative-add-extension',
options: {
debug: true,
},
},
],
}
In your loader chain:
FAQs
Chainable ESModule Loader to add missing file extensions to relative imports
The npm package esm-loader-import-relative-add-extension receives a total of 3 weekly downloads. As such, esm-loader-import-relative-add-extension popularity was classified as not popular.
We found that esm-loader-import-relative-add-extension demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.