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

Using node ESM modules #58

Closed
vonstring opened this issue Feb 28, 2023 · 3 comments
Closed

Using node ESM modules #58

vonstring opened this issue Feb 28, 2023 · 3 comments

Comments

@vonstring
Copy link

Is it possible at all to use ESM modules that use the node runtime? (e.g. tempy)

The approaches mentioned in the troubleshooting section fail in different ways, and I even tried exporting the import() function which made the entire CEP process crash.

Using import syntax with tempy, building fails with:

"promisify" is not exported by "__vite-browser-external", imported by "node_modules/tempy/index.js".
file: /Users/krhah771/code/esmtest/node_modules/tempy/index.js:5:8
3: import path from 'node:path';
4: import stream from 'node:stream';
5: import {promisify} from 'node:util';
           ^
6: import uniqueString from 'unique-string';
7: import tempDir from 'temp-dir';
error during build:
RollupError: "promisify" is not exported by "__vite-browser-external", imported by "node_modules/tempy/index.js".
    at error (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:2095:30)
    at Module.error (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:13145:16)
    at Module.traceVariable (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:13530:29)
    at ModuleScope.findVariable (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:12009:39)
    at Identifier.bind (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:7921:40)
    at CallExpression.bind (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:5716:23)
    at CallExpression.bind (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:9458:15)
    at VariableDeclarator.bind (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:5716:23)
    at VariableDeclaration.bind (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:5712:28)
    at Program.bind (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:5712:28)
    at Module.bindReferences (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:13141:18)
    at Graph.sortModules (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:24480:20)
    at Graph.build (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:24364:14)
    at async file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:25376:13
    at async catchUnfinishedHookActions (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:24534:20)
    at async rollupInternal (file:///Users/krhah771/code/esmtest/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:25371:5)
    at async build (file:///Users/krhah771/code/esmtest/node_modules/vite/dist/node/chunks/dep-ca21228b.js:44664:18)
    at async CAC.<anonymous> (file:///Users/krhah771/code/esmtest/node_modules/vite/dist/node/cli.js:813:9)

Using request syntax, the panel throws up this:

Uncaught Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/krhah771/code/esmtest/dist/cep/node_modules/tempy/index.js
require() of ES modules is not supported.
require() of /Users/krhah771/code/esmtest/dist/cep/node_modules/tempy/index.js from /main/index.html is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/krhah771/code/esmtest/dist/cep/node_modules/tempy/package.json.

    at __node_internal_captureLargerStackTrace (node:internal/errors:415:5)
    at __node_internal_addCodeToName (node:internal/errors:168:9)
    at new NodeError (node:internal/errors:329:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:13)
    at Module.load (node:internal/modules/cjs/loader:995:32)
    at Function.Module._load (node:internal/modules/cjs/loader:836:14)
    at Module.require (node:internal/modules/cjs/loader:1019:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at http://localhost:3000/main/main.tsx:27:15
@justintaylor-dev
Copy link
Contributor

I'm not sure that ESM Node.js modules will be compatible with Bolt since Vite will try and bundle anything using import and Node.js modules can't be bundled.

Most Node.js packages will ship with CJS bindings, but it looks like tempy does not. There may be a way to pull it off in the Vite config through resolve aliases.

I would say the easiest solution for this specific use case would be to simply use Node.js' built-in os.tempdir() function.

@vonstring
Copy link
Author

Okay, thanks for clarifying, and for the suggetions!

@justintaylor-dev
Copy link
Contributor

Sure thing, if you do find a possible solution feel free to open a PR.

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

No branches or pull requests

2 participants