Js file import error (Node)

Asked

Viewed 480 times

-1

I’m starting to learn a few things about Ode and for that I’m creating a chatbot using Blip. But I want to do this with an external js by making the connection. However I am having a problem trying to run my Ode apication. I’ll leave a print below to make it clearer.

inserir a descrição da imagem aqui

If it is not clear in the photo, this is the mistake:

PS C:\Testes\Take_chatbot> node index
C:\Testes\Take_chatbot\index.js:1

import * as BlipSdk from 'blip-sdk';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:895:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11
PS C:\Testes\Take_chatbot>

By my understanding js is not finding the two files I have listed in the Imports, but how can I resolve this error? These folders actually exist.

1 answer

1


Despite the operator import be defined in the Javascript syntax, the Nodejs engine, V8, does not yet implement this operator.

To import the module, you will need to use the function require as follows:

const BlipSdk = require('blip-sdk');

It is possible to use the operator import if you use a compiler that converts modern syntax to a Nodejs-compatible one, such as Babel.

Babel is mainly used to convert code that uses newer features into code that is compatible with older browsers. But if your code is only running on your computer, there may not be as much motivation to use this tool.

  • Great explanation. I understood and worked perfectly in my code. Thanks @user140828

Browser other questions tagged

You are not signed in. Login or sign up in order to post.