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

ES module support (ts-invariant\process) #227

Closed
tobiasdiez opened this issue Nov 5, 2021 · 3 comments · Fixed by #254
Closed

ES module support (ts-invariant\process) #227

tobiasdiez opened this issue Nov 5, 2021 · 3 comments · Fixed by #254

Comments

@tobiasdiez
Copy link

Loading ts-invariant in an ESM context, leads to the following error

(node:16684) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
D:\Programming\JabRefOnline\node_modules\ts-invariant\process\index.js:15
export function install() {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1025:15)
    at Module._compile (node:internal/modules/cjs/loader:1059:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
[vite dev] Error loading external "D:/Programming/JabRefOnline/node_modules/ts-invariant/process/index.js".
  at file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:19525:284
  at async __instantiateModule__ (file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:51609:3)
[vite dev] Error loading external "D:/Programming/JabRefOnline/node_modules/ts-invariant/process/index.js".
  at file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:19525:284
  at async __instantiateModule__ (file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:51609:3)

It works after one adds ts-invariant/process to the transpile option (or noExternal in vite ssr config). Such a workaround is not needed for the 'main' ts-invariant package. So I guess there is a problem with

@newbeea
Copy link

newbeea commented Dec 22, 2021

Loading ts-invariant in an ESM context, leads to the following error

(node:16684) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
D:\Programming\JabRefOnline\node_modules\ts-invariant\process\index.js:15
export function install() {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1025:15)
    at Module._compile (node:internal/modules/cjs/loader:1059:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
[vite dev] Error loading external "D:/Programming/JabRefOnline/node_modules/ts-invariant/process/index.js".
  at file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:19525:284
  at async __instantiateModule__ (file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:51609:3)
[vite dev] Error loading external "D:/Programming/JabRefOnline/node_modules/ts-invariant/process/index.js".
  at file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:19525:284
  at async __instantiateModule__ (file:///D:/Programming/JabRefOnline/.nuxt/dist/server/server.mjs:51609:3)

It works after one adds ts-invariant/process to the transpile option (or noExternal in vite ssr config). Such a workaround is not needed for the 'main' ts-invariant package. So I guess there is a problem with

same here when used in nuxt3

/node_modules/@apollo/client/utilities/globals/fix-graphql.js:1
import { remove } from "ts-invariant/process/index.js";
         ^^^^^^
SyntaxError: Named export 'remove' not found. The requested module 'ts-invariant/process/index.js' 
is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'ts-invariant/process/index.js';
const { remove } = pkg;

    at ModuleJob._instantiate (internal/modules/esm/module_job.js:120:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:165:5)
    at async Loader.import (internal/modules/esm/loader.js:177:24)
    at async __instantiateModule__ (file:///C:/Users/XPF/Desktop/workspace/nuxt/fp4-nuxt3/.nuxt/dist/server/server.mjs:4408:3)

I added “type": "module" to ts-invariant/process/package.json to workaround, but "adds ts-invariant/process to the transpile option" doesn't work for me, would you tell me more about your workaround? thanks!

@newbeea
Copy link

newbeea commented Dec 22, 2021

I find a pr #240

@ba55ie
Copy link

ba55ie commented Jan 20, 2022

I have the same problem when using ts-node. And I don't think ts-node has support for something like transpiling.

A quick fix is to rename the file and import to mjs: import { remove } from "ts-invariant/process/index.mjs";

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 a pull request may close this issue.

3 participants