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

🐛 BUG: Error: No such module "node:constants" #8245

Open
timkelty opened this issue Feb 25, 2025 · 2 comments
Open

🐛 BUG: Error: No such module "node:constants" #8245

timkelty opened this issue Feb 25, 2025 · 2 comments
Labels
awaiting Cloudflare response Awaiting response from workers-sdk maintainer team bug Something that isn't working

Comments

@timkelty
Copy link

Which Cloudflare product(s) does this pertain to?

Workers Vitest Integration

What versions are you using?

@cloudflare/vitest-pool-workers 0.6.16

What operating system and version are you using?

Mac 15.3.1 (24D70)

Please provide a link to a minimal reproduction

https://github.com/timkelty/cloudflare-workers-node-constants

Describe the Bug

When running tests, importing a dependency that imports from constants (example) will result in:

FAIL test/index.spec.ts [ test/index.spec.ts ]
Error: No such module "node:constants".
❯ Users/timkelty/Desktop/node-constants/node_modules/http-message-signatures/lib/algorithm/index.js?mf_vitest_no_cjs_esm_shim:5:21
❯ Users/timkelty/Desktop/node-constants/node_modules/http-message-signatures/lib/index.js?mf_vitest_no_cjs_esm_shim:30:14

  • I've tried Depency pre-building without any luck
  • This only happens during vitest, wrangler deploy works fine.

Please provide any relevant error logs

No response

@timkelty timkelty added the bug Something that isn't working label Feb 25, 2025
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Feb 25, 2025
@emily-shen
Copy link
Contributor

Hey! You need to build with wrangler deploy --dry-run --outdir=dist and set your vitest config to use the built output - this will make sure wrangler bundles the node polyfills in.

I think we're working on a smoother workflow though - cc @edmundhung ?

@emily-shen emily-shen added the awaiting reporter response Needs clarification or followup from OP label Feb 25, 2025
@timkelty
Copy link
Author

timkelty commented Feb 25, 2025

Thanks @emily-shen! Getting closer:

Here's what I tried:

// vitest.config.ts
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersProject({
	test: {
		poolOptions: {
			workers: {
				main: "./dist/index.js",
				wrangler: {
					configPath: "./wrangler.jsonc",
					environment: "production",
				},
				miniflare: {
					kvNamespaces: ["CRAFT_CLOUD_HOSTS"],
				},
			},
		},
	},
});

With this in package.json:scripts

		"build": "wrangler deploy --dry-run --outdir ./dist",
		"pretest": "npm run build",
		"test": "vitest",

With that, I still get the error.
However, if I convert it to an async import (await import('http-message-signatures')), it works fine. Note: I tried this approach before setting main: "./dist/index.js", and it didn't help.

@emily-shen emily-shen added awaiting Cloudflare response Awaiting response from workers-sdk maintainer team and removed awaiting reporter response Needs clarification or followup from OP labels Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting Cloudflare response Awaiting response from workers-sdk maintainer team bug Something that isn't working
Projects
Status: Untriaged
Development

No branches or pull requests

2 participants