1

Im trying to make a Gitlab monitor using javascript and gitlab api. I do install Gitlab API using

npm install @gitbeaker/node # NodeJS default, index.es.js for esm

and I have a index.html with a main.js looking like followings

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>This is my monitor</h1>
    <script type="module" src="main.js"></script>
</body>

</html>

main.js

import { Gitlab } from '@gitbeaker/node';
console.log('okej');

installing the gitlab api gives a folder named node_modules containing many files. I do a:

node main.js

and I get following error:

import { Gitlab } from '/node_modules/@gitbeaker/browser/';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1053:16)
    at Module._compile (internal/modules/cjs/loader.js:1101:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

to be on sure side I tried with to launch the index.html file with live server and seeing in the console it says:

TypeError: Error resolving module specifier: @gitbeaker/node

I don't know what is wrong here. I use npm version 6.14.6 and node version v12.18.3

The instruction I follow for Gitlab API is:https://github.com/jdalrymple/gitbeaker

Payam30
  • 689
  • 1
  • 5
  • 20

1 Answers1

0

Either use require instead of import or use a transpiler like babel.

const { Gitlab } = require('@gitbeaker/node');

you can also rename your file extension to .mjs and it should work when enabling experimental flag.

node --experimental-modules main.mjs

For node >= 13 you can enable modules from package.json

You can find more info here How can I use an es6 import in node?

Antoine Eskaros
  • 836
  • 5
  • 22