XCP-ng
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    SyntaxError: Cannot use import statement outside a module

    Scheduled Pinned Locked Moved Solved Xen Orchestra
    14 Posts 6 Posters 3.8k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • aqua-calcA Offline
      aqua-calc @olivierlambert
      last edited by

      @olivierlambert Remove how?

      1 Reply Last reply Reply Quote 0
      • olivierlambertO Offline
        olivierlambert Vates 🪐 Co-Founder CEO
        last edited by

        rm -rf node_modules?

        aqua-calcA 1 Reply Last reply Reply Quote 0
        • aqua-calcA Offline
          aqua-calc @olivierlambert
          last edited by aqua-calc

          @olivierlambert Still getting the same error:

          root@xenorchestra:/opt/xen-orchestra/packages/xo-server# yarn start
          yarn run v1.22.5
          $ node dist/cli.mjs
          (node:3490) 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)
          /opt/xen-orchestra/packages/xo-server/dist/xapi/mixins/index.js:2
          import _gpu, * as __gpu from "./gpu";
          ^^^^^^
          
          SyntaxError: Cannot use import statement outside a module
              at wrapSafe (internal/modules/cjs/loader.js:984:16)
              at Module._compile (internal/modules/cjs/loader.js:1032:27)
              at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
              at Module.load (internal/modules/cjs/loader.js:933:32)
              at Function.Module._load (internal/modules/cjs/loader.js:774:14)
              at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:199:29)
              at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
              at async Loader.import (internal/modules/esm/loader.js:177:24)
              at async Object.loadESM (internal/process/esm_loader.js:68:5)
          error Command failed with exit code 1.
          info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
          
          julien-fJ 1 Reply Last reply Reply Quote 0
          • olivierlambertO Offline
            olivierlambert Vates 🪐 Co-Founder CEO
            last edited by olivierlambert

            Then I don't know. @julien-f might be around tomorrow to check if it rings any bell.

            edit: as usual, double check you are on latest commit from master.

            1 Reply Last reply Reply Quote 0
            • julien-fJ Offline
              julien-f Vates 🪐 Co-Founder XO Team @aqua-calc
              last edited by

              @aqua-calc Make sure the repo is clean and there are no untracked files (git status).

              aqua-calcA 1 Reply Last reply Reply Quote 1
              • aqua-calcA Offline
                aqua-calc @julien-f
                last edited by

                @julien-f I did fresh install, with config exported from the original xenorchestra server and then imported into the new server

                J 1 Reply Last reply Reply Quote 0
                • J Offline
                  jmrapin @aqua-calc
                  last edited by jmrapin

                  @aqua-calc, I have the same issue....
                  building at commit level a204b6fb3f1236dc5f4519a734c4d5c8f99f9e9c
                  and I am getting the same error. Cleaned all a did a fresh git pull --ff-only

                  yarn run v1.22.5
                  $ node dist/cli.mjs
                  (node:3448) 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)
                  /root/xen-orchestra/packages/xo-server/dist/xapi/mixins/index.js:2
                  import _gpu, * as __gpu from "./gpu";
                  ^^^^^^

                  SyntaxError: Cannot use import statement outside a module
                  etc...

                  The yarn build command produces those files (there is something wrong somewhere, why does the build produces index.js when there is the index.mjs?)

                  So

                  1. delete /packages/xo-server/dist/xapi/mixins/index.js (rm index.js)
                  2. delete /packages/xo-server/dist/xapi/mixins/index.js.map (rm index.js.map)
                  3. remove the import and export statements referencing index.js from file /packages/xo-server/dist/xapi/mixins/index.mjs (nano index.mjs)

                  and did the same as above for /packages/xo-server/dist/api/index.js
                  and same for /packages/xo-server/dist/xo-mixins/index.js

                  And then yarn start and it starts and orchestra is fine as far as I can see.

                  Not sure if that's the best but it got me going . Hope this helps someone.

                  julien-fJ aqua-calcA I 3 Replies Last reply Reply Quote 0
                  • julien-fJ Offline
                    julien-f Vates 🪐 Co-Founder XO Team @jmrapin
                    last edited by

                    @jmrapin You still hav untracked index.js files in xo-server/src, please remove them and rebuild.

                    J 1 Reply Last reply Reply Quote 0
                    • J Offline
                      jmrapin @julien-f
                      last edited by

                      @julien-f well spotted. Thanks!
                      All good now.

                      1 Reply Last reply Reply Quote 0
                      • aqua-calcA Offline
                        aqua-calc @jmrapin
                        last edited by

                        @jmrapin Thank you!

                        1 Reply Last reply Reply Quote 0
                        • I Offline
                          iron_man @jmrapin
                          last edited by

                          @jmrapin good job! after removing the untracked files, problem wasn't solved, and then i followed your instructions and it worked, thanks a lot!

                          1 Reply Last reply Reply Quote 0
                          • ronaldgevernR Offline
                            ronaldgevern
                            last edited by

                            This error can happen in different cases depending on whether you're working with JavaScript on the server-side with Node.js , or client-side in the browser. There are several reasons behind "Cannot use import statement outside a module" error, and the solution depends on how you call the module or script tag.

                            Add type="module" inside the script tag

                            When working with ECMAScript modules and JavaScript module import statements in the browser, you'll need to explicitly tell the browser that a script is module. To do this, you have to add type="module" onto any ‹script› tags that point to a JavaScript module. Once you do this you can import that module without issues.

                            <script type="module" src="./index.js"></script>
                            

                            If you are working on Node.js or react applications and using import statements instead of require to load the modules, then ensure your package.json has a property "type": "module" as shown below.

                            {
                              // ...
                              "type": "module",
                              // ...
                            }
                            
                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post