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

The requested module '@featurevisor/sdk' does not provide an export named 'createInstance' on Node 20 #269

Open
ghost opened this issue Mar 1, 2024 · 26 comments · Fixed by featurevisor/featurevisor-example-nuxt#1

Comments

@ghost
Copy link

ghost commented Mar 1, 2024

import { createInstance } from '@featurevisor/sdk';
         ^

SyntaxError: The requested module '@featurevisor/sdk' does not provide an export named 'createInstance'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

On Node.js v20.10.0

How can I fix it, or do you need to up the nodejs version on this package?

@ghost
Copy link
Author

ghost commented Mar 1, 2024

fixed

import CreateInstance from '@featurevisor/sdk';
const { createInstance } = CreateInstance;

@ghost ghost closed this as completed Mar 1, 2024
@fahad19
Copy link
Member

fahad19 commented Mar 1, 2024

@anatoliidaostreet: could you please share a full reproduction of the issue somehow?

I would still expect import { createInstance } from "@featurevisors/sdk"; work in Node.js v20+

Are you using .mjs files in your application?

@ghost
Copy link
Author

ghost commented Mar 1, 2024

@anatoliidaostreet: could you please share a full reproduction of the issue somehow?

I would still expect import { createInstance } from "@featurevisors/sdk"; work in Node.js v20+

Are you using .mjs files in your application?

Hello, @fahad19
I use

  • .ts
  • "type": "module",

tsconfig.json

{
    "compilerOptions": {
        "target": "es2017",
        "types": ["jest", "node"],
        "typeRoots": ["node_modules/@types", "typings"],
        "lib": ["es2015", "es2016", "es2017", "es2018", "dom"],
        "module": "ESNext",
        "moduleResolution": "node",
        "sourceMap": true,
        "declaration": true,
        "esModuleInterop": true,
        "downlevelIteration": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "allowJs": false,
        "skipLibCheck": true,
        "allowSyntheticDefaultImports": true,
        "resolveJsonModule": true,
        "importHelpers": true,
        "baseUrl": "src",
        "outDir": "./dist",
        "paths": {
            "@/*": ["*"],
            "@config": ["config"],
            "@controllers/*": ["controllers/*"],
            "@exceptions/*": ["exceptions/*"],
            "@interfaces/*": ["interfaces/*"],
            "@middlewares/*": ["middlewares/*"],
            "@routes/*": ["routes/*"],
            "@services/*": ["services/*"],
            "@utils/*": ["utils/*"]
        }
    },
    "files": ["custom.d.ts, @featurevisor/sdk"],
    "include": ["src", "src/**/*.ts", ".env", "custom.d.ts", "custom.d.ts"],
    "exclude": [
        "dist",
        "node_modules",
        "test/**/*",
        "ci/**/*",
        "scripts/**/*",
        "__mocks__/**/*",
        "**/node_modules/**/*",
        "**/dts/**/*",
        "**/dist/**/*",
        "**/*.(spec|test).ts",
        "logs"
    ],
    "compileOnSave": true,
    "typeAcquisition": {
        "enable": true
    }
}

I hope it will help

@fahad19
Copy link
Member

fahad19 commented Mar 1, 2024

thanks @anatoliidaostreet!

I think the issue is similar as described under "Named Import" section here: https://dev.to/lico/how-to-import-and-export-in-commonjs-and-es-modules-43m1

It has same error message like your original comment above.

May be @featurevisor/sdk's package.json needs a new "type": "module", and that will solve the problem.

I will try it out soon to fix it properly. Reopening.

@fahad19 fahad19 reopened this Mar 1, 2024
@meirroth
Copy link
Contributor

@fahad19 I got the same error when building my nuxt project. You can reproduce the issue with the nuxt example repo, just clone and run build.

@meirroth
Copy link
Contributor

fixed

import CreateInstance from '@featurevisor/sdk';
const { createInstance } = CreateInstance;

@fahad19 The above solution didn't help, nor did adding "type": "module" to @featurevisor/sdk's package.json. Any idea?

@fahad19
Copy link
Member

fahad19 commented Mar 17, 2024

@meirroth: I just cloned the repo again and it's working for me: https://github.com/featurevisor/featurevisor-example-nuxt

only different was I did npm ci instead of npm install as suggested in README.

could you please confirm if there's any different in behaviour between npm ci and npm install for you?

@fahad19
Copy link
Member

fahad19 commented Mar 17, 2024

locally I am on Node.js v18 though. may be the issue is happening from v20 onwards 🤔

@meirroth
Copy link
Contributor

meirroth commented Mar 18, 2024 via email

@meirroth
Copy link
Contributor

BTW, I tried npm ci and didn't help.

@fahad19
Copy link
Member

fahad19 commented Mar 22, 2024

I tried to reproduce the problem here: https://github.com/featurevisor/featurevisor-example-nodejs

It works when I use require() instead of import: https://github.com/featurevisor/featurevisor-example-nodejs/blob/main/src/index.js#L2

GitHub Actions output for verification: https://github.com/featurevisor/featurevisor-example-nodejs/actions/runs/8397305039/job/23000378157#step:5:5

I will look into the import part next.

@fahad19
Copy link
Member

fahad19 commented Mar 22, 2024

this PR is where I am collecting errors and possible solutions when using imports: featurevisor/featurevisor-example-nodejs#1

@fahad19
Copy link
Member

fahad19 commented Mar 22, 2024

@meirroth: I merged the PR featurevisor/featurevisor-example-nodejs#1

please let me know if your problem is solved by this.

@fahad19
Copy link
Member

fahad19 commented Mar 23, 2024

@meirroth
Copy link
Contributor

Hi @fahad19, thanks for looking into this!
I see the nodejs example works, however, it didn't fix the issue in the nuxt example.
Getting the following build time error with your suggested update:

ERROR  "default" is not exported by "node_modules/@featurevisor/sdk/lib/index.js", imported by "featurevisor.ts".                                                                 
file: /workspaces/featurevisor-example-nuxt/featurevisor.ts:1:7
1: import FeaturevisorSDK, { FeaturevisorInstance } from "@featurevisor/sdk";
          ^
2: const { createInstance } = FeaturevisorSDK;

Try running the PR featurevisor/featurevisor-example-nuxt#1

@fahad19
Copy link
Member

fahad19 commented Mar 24, 2024

@meirroth: I tried running the Nuxt example locally with Node.js v20. Could both run the dev server, and also build locally.

Setup GitHub Actions as well to show it's working there: featurevisor/featurevisor-example-nuxt#2

I am not sure what could possibly be different in your setup 🤔

@meirroth
Copy link
Contributor

meirroth commented Mar 24, 2024

@fahad19 Interesting.

The Nuxt example (main branch) builds and runs dev OK, so the GitHub action won't show you any issue. The error shows only when you run build and open localhost in the browser (running on node v20). I'm sorry I didn't make this clear earlier.

The PR with your suggested fix errors on build.

@fahad19
Copy link
Member

fahad19 commented Mar 25, 2024

The error shows only when you run build and open localhost in the browser (running on node v20)

I tried the following:

cannot seem to reproduce your issue at all from my side, @meirroth :(

@meirroth
Copy link
Contributor

meirroth commented Mar 26, 2024

@fahad19 I appreciate your patience while investigating this! Here are the steps to reproduce the error:

  1. Make sure you're running node v20.
  2. Clone main branch of nuxt example.
  3. Run npm ci
  4. Run npm run build
  5. Run node .output/server/index.mjs
  6. Open localhost in the browser.
  7. See 500 This page is temporarily unavailable. in browser, and the following error in the terminal:
[nuxt] [request error] [unhandled] [500] Named export 'createInstance' not found. The requested module '@featurevisor/sdk' 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 '@featurevisor/sdk';
const { createInstance } = pkg;

@yoshrubin
Copy link

yoshrubin commented Apr 11, 2024

I was able to reproduce the issue and adding

  build: {
    transpile: ["@featurevisor/sdk"],
  },

to the nuxt.config file fixed it

@meirroth
Copy link
Contributor

Thank you @yoshrubin, that fixed it!

@fahad19 I updated the PR with this fix featurevisor/featurevisor-example-nuxt#1

@fahad19
Copy link
Member

fahad19 commented Apr 11, 2024

Excellent stuff!

I'll be able to merge by Sunday.

I believe we can also update the Nuxt integration docs page available in this repository here.

Once everything is updated, we can close the issue after.

Great collaboration from all parties 🙌

@meirroth
Copy link
Contributor

Thank you

It should be noted that this is a workaround not a fix. See response from Nuxt team nuxt/nuxt#26375 (comment)

@fahad19
Copy link
Member

fahad19 commented Apr 14, 2024

thanks @meirroth. keeping it open until we get to the bottom of this.

your PR in other repo has been merged.

I cannot seem to understand yet what I need to change in the @featurevisor/sdk package to make it convenient for others.

@fahad19 fahad19 reopened this Apr 14, 2024
@meirroth
Copy link
Contributor

@fahad19 I wish I knew. I messaged Daniel on the Nuxt repo.

@meirroth
Copy link
Contributor

@fahad19 I came across this documentation which may help https://nuxt.com/docs/guide/concepts/esm#troubleshooting-esm-issues

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