esm-loader-import-meta-custom
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

esm-loader-import-meta-custom

Node.js ESModule Loader to stub custom import.meta.* properties.

This loader will ignore any valid import.meta properties, such as:

  • import.meta.resolve
  • import.meta.url

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.

Usage

Some web frameworks will override import.meta with custom properties, not known to Node.js. Your production web code might contain something like this:

// index.js
const dbUrl = import.meta.env.VITE_DATABASE_URL

If you try testing this web code in isolation under Node.js it will fail, as Node does not recoginze import.meta.env*.

Chainable

This loader can be configured, and chained with other loaders, using node-esm-loader.

npm install --save-dev esm-loader-import-meta-custom node-esm-loader
// .loaderrc.js
export default {
  loaders: [
    {
      loader: 'esm-loader-import-meta-custom',
      options: {
        meta: {
          env: {
            VITE_DATABASE_URL: 'postgres://user@localhost/db',
          },
        },
      },
    },
  ],
}
# node >= 20.7
NODE_OPTIONS="--import node-esm-loader/register" node index.js

# node < 20.7
NODE_OPTIONS="--loader node-esm-loader" node index.js

Options

Meta

See example above.

Debug
// .loaderrc.js
export default {
  loaders: [
    {
      loader: 'esm-loader-import-meta-custom',
      options: {
        debug: true,
      },
    },
  ],
}

License

MIT

Package Sidebar

Install

npm i esm-loader-import-meta-custom

Weekly Downloads

1

Version

1.0.6

License

MIT

Unpacked Size

5.47 kB

Total Files

4

Last publish

Collaborators

  • brev