question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node.js Core Module Support

See original GitHub issue

Clear and concise description of the problem

This might seem like a very weird use case for Vite, but I’ve been experimenting with developing a framework to develop normal Node.js libraries or even CLIs using Vite as the core. Hugely inspired by how Vitest works with vite-node, it aims to centralise all the previous build tools (tsc, rollup, testing etc.) into one convenient package using the same Vite config. Plain, simple and easy to setup a modern ESM-first TS project with far less dependencies to go around. So far, the watch mode uses the normal esbuild transforms, rollup for production etc. Essentially a modern TSDX replacement, I’ve been pleasantly surprised that Vite makes a good fit even if it isn’t the original intended use case.

Which leads to one slight hiccup. Vite does not really support any libraries that call Node.js core modules since the intended target is browsers only.

For example, trying to develop a CLI using cac returns the following error when running vite build:

1: import { EventEmitter } from 'events';
            ^
error during build:
Error: 'EventEmitter' is not exported by __vite-browser-external, imported by ../../node_modules/cac/dist/index.mjs

A workaround is to update rollupOptions.external to include the "events" library (and also "node:events"). This isn’t ideal considering there are many core Node.js modules, thus it’d be better to resolve this upstream.

Suggested solution

My proposal is to add an additional option in the Vite config that allows the resolver to ignore any imports that call the Node.js built-in modules. It could probably be a boolean value with the key node?

I feel an option like that could also let a different type of ecosystem to thrive and we may see more interesting Node-based projects using Vite, especially due to vite-node.

Alternative

https://github.com/vitejs/vite/issues/2694#issuecomment-826195660 is the most similar issue I’ve found, although the solution is not optimal in the above-given context as the libraries are not intended for the browser.

Additional context

No response

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:9
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
bluwycommented, Apr 20, 2022

A workaround is to update rollupOptions.external to include the "events" library (and also "node:events"). This isn’t ideal considering there are many core Node.js modules, thus it’d be better to resolve this upstream.

What about using import { builtinModules } from 'module' and pass the list of nodejs builtin modules to rolllupOptions.external? I’m not sure it’s good to officially support this usecase yet since it’s a niche one. And it may be better done externally first too to quickly make changes. Until it’s stablized (e.g. vite-node), there may be a chance Vite could officially support it.

2reactions
abarkecommented, May 31, 2022

I could imagine that if target: "node12" is set that esbuild takes over the build instead of rollup in this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommonJS modules | Node.js v19.3.0 Documentation
CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and ......
Read more >
Node.js Modules - TutorialsTeacher
Module in Node.js is a simple or complex functionality organized in a single or ... The core modules include bare minimum functionalities of...
Read more >
The Node Core Modules - Flavio Copes
A list of the core modules provided by Node.js. ... util, supports the needs of Node.js internal APIs, useful for application and module...
Read more >
Node.js Built-in Modules - W3Schools
js has a set of built-in modules which you can use without any further installation. Here is a list of the built-in modules...
Read more >
Exploring Node.js Core Modules - Dot Net Tricks
It is used to support cryptography for encryption and decryption. It can be accessed with require('crypto'). Debugger. It is used for code ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found