intercom / intercom-node

Node.js bindings for the Intercom API
https://developers.intercom.com
Other
366 stars 116 forks source link

Try to run boiler plate code in a node script either results in "SyntaxError: Cannot use import statement outside a module" or "Error [ERR_MODULE_NOT_FOUND]" #355

Closed garethbrickman closed 1 year ago

garethbrickman commented 1 year ago

Version info

Expected behavior

Running my node script will execute the boiler plate code

Actual behavior

Errors originating from the import statement.

./intercom-chat-data-pull.js
/Users/garethbrickmansigma/GitHub/technical-support/intercom-scripts/JavaScript/intercom-chat-data-pull.js:3
import { Client } from './dist/index';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

If I add "type": "module" in the package.json, then the error becomes:

./intercom-chat-data-pull.js
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/garethbrickmansigma/GitHub/technical-support/intercom-scripts/JavaScript/dist/index' imported from /Users/garethbrickmansigma/GitHub/technical-support/intercom-scripts/JavaScript/intercom-chat-data-pull.js
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:416:11)
    at moduleResolve (node:internal/modules/esm/resolve:932:10)
    at defaultResolve (node:internal/modules/esm/resolve:1044:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

I don't understand where './dist/index' would be located? It does not get created after I run yarn add intercom-client

My script's code for intercom-chat-data-pull.js:

#!/usr/bin/env node

import { Client } from './dist/index';

const client = new Client({ tokenAuth: { token: process.env.INTERCOM_API_TOKEN } });
client.useRequestOpts({
    headers: {
        'Intercom-Version': 2.5,
    }
});
garethbrickman commented 1 year ago

I got it to work with:

const { Client } = require('./node_modules/intercom-client/dist/index.js');