featurevisor / featurevisor

Feature flags, experiments, and remote config management with GitOps

Home Page:https://featurevisor.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

opened this issue · comments

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?

fixed

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

@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?

@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

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 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.

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?

@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?

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

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

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

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

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

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

@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 🤔

@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.

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 :(

@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;

I was able to reproduce the issue and adding

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

to the nuxt.config file fixed it

Thank you @yoshrubin, that fixed it!

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

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 🙌

Thank you

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

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 I wish I knew. I messaged Daniel on the Nuxt repo.