Skip to content

Add --experimental-vm-modules flag to bin/harper#237

Open
cb1kenobi wants to merge 2 commits intomainfrom
enable-vm-modules
Open

Add --experimental-vm-modules flag to bin/harper#237
cb1kenobi wants to merge 2 commits intomainfrom
enable-vm-modules

Conversation

@cb1kenobi
Copy link
Contributor

When running harper dev from an app, it throws the following:

Error: node_vm_1.SourceTextModule is not a constructor
    at createModule (/Users/chris/projects/harper/security/jsLoader.ts:285:14)
    at async getOrCreateModule (/Users/chris/projects/harper/security/jsLoader.ts:235:18)
    at async loadModuleWithCache (/Users/chris/projects/harper/security/jsLoader.ts:245:18)
    at async loadModuleWithVM (/Users/chris/projects/harper/security/jsLoader.ts:349:22)
    at async scopedImport (/Users/chris/projects/harper/security/jsLoader.ts:93:11)
    at async loadComponent (/Users/chris/projects/harper/components/componentLoader.ts:498:28)
    at async loadComponent (/Users/chris/projects/harper/components/componentLoader.ts:337:26)
    at async Promise.all (index 0)
    at async loadRootComponents (/Users/chris/projects/harper/server/loadRootComponents.js:36:3)
    at async startHTTPThreads (/Users/chris/projects/harper/server/threads/socketRouter.ts:49:4)

The problem is vm.SourceTextModule is only available when --experimental-vm-modules is set. This flag is set when the worker is started, however the components (e.g. my app) is loaded on the main thread before start worker is called.

@cb1kenobi cb1kenobi requested a review from a team as a code owner March 17, 2026 00:55
@kriszyp
Copy link
Member

kriszyp commented Mar 17, 2026

I am really curious how to reproduce this? resources.isWorker is supposed to be false on the main thread on line 392 here: https://github.com/HarperFast/harper/blob/main/components/componentLoader.ts#L392
Which is supposed to prevent any application code from running on the main thread. In fact, if you can get application code running on the main thread, that is real bug. That should only happen if you set the worker thread count to zero, which is supposed to enter a special "lite" mode (https://github.com/HarperFast/harper/blob/main/server/threads/socketRouter.ts#L45), which isn't really supported (other than for unit tests).

@cb1kenobi
Copy link
Contributor Author

I ran npm create harper@latest, choose to NOT start harper after installing npm deps, cd into the project. Make sure your harper points to v5. I nuked my ~/harper dir. Run harper dev.

P.S. I'm working on fixing the tests.

@kriszyp
Copy link
Member

kriszyp commented Mar 17, 2026

Oh, I see, this is because extension modules are running on the main thread. I think this is root issue, and we should look at preventing this altogether.
And I thought you couldn't pass in node args in the header like this?
And can you file a ticket for the issue?

@@ -1,4 +1,4 @@
#!/usr/bin/env node
#!/usr/bin/env node --experimental-vm-modules
Copy link
Member

@kriszyp kriszyp Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the correct syntax is:
#!/usr/bin/env -S node --experimental-vm-modules
But I have no idea if that is really cross-platform/OS safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-S isn't strictly necessary if there's only a single argument.

});

// If not safe mode, components are loaded on the main thread which requires VM modules to be enabled
if (!process.env.HARPER_SAFE_MODE && !process.execArgv.includes('--experimental-vm-modules')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to disable VM loading in jsLoader.ts when this condition occurs, not exit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hear you. This is a toughy. If you run node dist/bin/harper.js, then it will have different behavior than if you run harper via npm or global link.

@cb1kenobi
Copy link
Contributor Author

I will update this PR to:

  • add the -S to the shebang
  • remove the exit if --experimental-vm-modules flag is not set
  • use the non-sandboxed js loader if vm modules are not available (which is what v4 does)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants