Skip to content
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

access the import cache imported packages cache like require.cache #45207

Closed
ganeshkbhat opened this issue Oct 27, 2022 · 7 comments
Closed

access the import cache imported packages cache like require.cache #45207

ganeshkbhat opened this issue Oct 27, 2022 · 7 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. stale

Comments

@ganeshkbhat
Copy link

ganeshkbhat commented Oct 27, 2022

access the import cache - imported packages cache like require.cache

What is the problem this feature will solve?

accessing cache will give access to modules imported in a module or a different module. node.js does not have an ast support while languages like python have it. to access modules imported is there a simple hack or a way to access the import cache. it could also be a immutble cache as well.

What is the feature you are proposing to solve the problem?

to access import cache to fetch the modules imported. is there a simple hack or a way to access the import cache. it could also be a immutble cache as well.

Affected URL(s)

https://nodejs.org/api/esm.html#esm_no_require_cache

Description of the problem

How do i access the imported packages cache like require.cache?

I am trying to do something like this:

c.mjs

const tester = import("./test.js");
import * as fs from "fs";
import console from 'console';

// !!import.meta.cache is not an API as per docs - 
// https://github.com/nodejs/help/issues/2806
// How do i access the imported packages cache like require.cache
if (!!import.meta && !!import.meta.cache) {
    for (let p in import.meta.cache) {
        console.log(p);
        // console.log(trim(p));
    }
}

c.js

const tester = require("./test.js");

// current access of require.cache i am using
if (!!require.cache) {
    for (let p in require.cache) {
        console.log(p);
        // console.log(trim(p));
    }
}


function trim(p) {
    var re = /(.*?).js/;
    var basename = path.basename(p);
    var moduleName = re.exec(basename)[1];
    return [moduleName, p];
}

test.js

var path = require('path');
var c = require('fs');
var f = require('child_process');

This question comes after

https://stackoverflow.com/questions/74215997/how-do-i-do-console-log-in-esm-or-es6-file-without-having-a-package-json-in-the?noredirect=1#comment131031974_74215997

nodejs/help#2806 (comment)

nodejs/help#2806

Note:
I have read this:

I am aware of this docs - https://nodejs.org/api/esm.html#esm_no_require_cache

A simple hack is also fine.

What alternatives have you considered?

since there is no access to ast, th the only other way is to use a regex to access the import statements which is very crude and can be a serious mistake for the regex parser.

@ganeshkbhat ganeshkbhat added the feature request Issues that request new features to be added to Node.js. label Oct 27, 2022
@MoLow MoLow added the esm Issues and PRs related to the ECMAScript Modules implementation. label Oct 27, 2022
@aduh95
Copy link
Contributor

aduh95 commented Oct 28, 2022

Duplicate of #45206?

@ganeshkbhat
Copy link
Author

ganeshkbhat commented Oct 30, 2022

documenting two hacks below. however, i wish the imports cache was exposed as require.cache atleast for nodejs, bun, etc environments, if not for browsers.

the exposure of ast (atleast for nodejs, bun, etc environments, if not for browsers) may be the best solution though for which i am intending to make a proposal. #45158

@ganeshkbhat
Copy link
Author

ganeshkbhat commented Oct 30, 2022

  • hack one:
    Use a module loader like below node --experimental-modules ./hack-loader.mjs file.mjs
// hack-loader.mjs
globalThis.loadedURLs = [];
export function load(url, context, next) { globalThis.loadedURLs.push(url); return next(url, context); }

#45206 (comment)

  • hack two:
    using a require/ import regex to check number of import statements, the modules and objects being accessed. here is the code base i am using for regex access:

import object from "module" - /import(?:[\s.*]([\w*{}\n\r\t, ]+)[\s*]from)?[\s*](?:["'](.*[\w]+)["'])?/gm

import("module") - /import\((?:["'\s]*([\w*{}\n\r\t, ]+)\s*)?["'\s](.*([@\w_-]+))["'\s].*\);$/mg + /(?:import\('?"?)(.*?)(?:'?"?\))/

require("module") - /(?:require\('?"?)(.*?)(?:'?"?\))/

https://github.com/ganeshkbhat/get-imports/blob/main/index.js

@mardybardy
Copy link

Posted a hacky solution for anyone needing something in the meantime here, no idea how robust it is but meeting my needs for the time being:
nodejs/help#2806 (comment)

@ganeshkbhat
Copy link
Author

Nice. I published a package for the same - https://www.npmjs.com/package/get-imported welcome to use.

Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Jan 24, 2024
Copy link
Contributor

There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. stale
Projects
None yet
Development

No branches or pull requests

4 participants