-
Notifications
You must be signed in to change notification settings - Fork 128
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
Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/pocketbase/dist/pocketbase.es.mjs not supported. #34
Comments
If you want to use the es bundle you need to add a If you want to use the common js bundle you have to import it like this (note the const PocketBase = require('pocketbase/cjs'); If you are using an older node version, you may also need to load some polyfills. You could find more info in the "Installation" section - https://github.com/pocketbase/js-sdk#installation. |
@RyanB28 I'll close the issue since the above should allow you to load the sdk as commonjs module (although I recommend to switch to the ES bundle because you'll be able to use also the other exports like the DTOs). If you are still unable resolve the issue, please comment with your node version and how you load the SDK and II'll investigate it further. |
If I add Type Module, I get "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"". |
@RyanB28 Ah, I see. You may need to configure your ts compiler options per TypeStrong/ts-node#1007, so something like this may work (I haven't tested it):
Or you can also refer to this SO answer - https://stackoverflow.com/questions/62096269/cant-run-my-node-js-typescript-project-typeerror-err-unknown-file-extension#answer-71426203. Using the CommonJS module is fine, but keep in mind that a lot of new packages are moving to ESM (even browsers now supports them) and you may not be able to use them. |
You are a HERO! Thank you |
For anyone flying by for the lookout of a quickfix: In my case I was working in a nx-monorepo where this problem destroyed my precious time. No fixes helped like changing the Here is my quick and somewhat dirty option:
"exports": {
".": "./dist/pocketbase.es.mjs",
"./cjs": "./dist/pocketbase.cjs.js",
"./umd": "./dist/pocketbase.umd.js"
} to this: "exports": {
".": "./dist/pocketbase.cjs.js",
"./cjs": "./dist/pocketbase.cjs.js",
"./umd": "./dist/pocketbase.umd.js"
} If you dont do this and just to import pocketbase like this Important: If you update your packages you will probably do this again. I know this is not ideal. However in my situation I was not able to go for |
solution from pocketbase#34 (comment)
solution from pocketbase#34 (comment)
Thanks for that @Megajin, that solved my issue too. I'm using this with nextjs, and everything was working fine importing from the ESM version. However, I also run some administrative scripts using ts-node, and for some reason ts-node has huge issues with this, even though nextjs was totally fine and was using the same tsconfig and package.json. It's crazy black magic this ESM business. For anyone wanting to reference it, here is the ts-node issue (though no amount of tinkering with the suggested solutions/workarounds fixed it for me): |
solution from pocketbase#34 (comment)
solution from pocketbase#34 (comment)
The title says almost all. I am using ESM.
Package.json
{ "name": "NotNeeded", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start:dev": "npx ts-node ./src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "@types/puppeteer": "^5.4.6", "pocketbase": "^0.7.0", "puppeteer": "^17.1.3" }, "devDependencies": { "typescript": "^4.8.3" } }
tsconfig.json
`
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
}
}
`
The full error is
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/pocketbase/dist/pocketbase.es.mjs not supported. Instead change the require of .../node_modules/pocketbase/dist/pocketbase.es.mjs to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (.../TheScraper/src/controller/pocketbaseController.ts:12:22) at m._compile (/opt/homebrew/lib/node_modules/ts-node/dist/index.js:857:29) at require.extensions.<computed> [as .ts] (/opt/homebrew/lib/node_modules/ts-node/dist/index.js:859:16) at Object.<anonymous> (.../TheScraper/src/index.ts:12:32) at m._compile (/opt/homebrew/lib/node_modules/ts-node/dist/index.js:857:29) at require.extensions.<computed> [as .ts] (/opt/homebrew/lib/node_modules/ts-node/dist/index.js:859:16) at phase4 (/opt/homebrew/lib/node_modules/ts-node/dist/bin.js:466:20) at bootstrap (/opt/homebrew/lib/node_modules/ts-node/dist/bin.js:54:12) at main (/opt/homebrew/lib/node_modules/ts-node/dist/bin.js:33:12) at Object.<anonymous> (/opt/homebrew/lib/node_modules/ts-node/dist/bin.js:579:5) { code: 'ERR_REQUIRE_ESM' }
Am I doing something wrong? Or is it a bug?
The text was updated successfully, but these errors were encountered: