Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble integrating ES6 module in NodeJS #46

Open
mvermand22 opened this issue Feb 16, 2022 · 2 comments
Open

Trouble integrating ES6 module in NodeJS #46

mvermand22 opened this issue Feb 16, 2022 · 2 comments

Comments

@mvermand22
Copy link

Can you please provide a full working example of how to use the library in NodeJS?
I do not succeed in loading the library.

I always get errors such as

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by
 the default ESM loader

or
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

This is my tsconfig.json:

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES6",
    "outDir": "./dist",
  },
  "exclude": [
    "node_modules"
  ]
}

I tried to add and remove the type = "module" line in package.json.
I tried

let PushBullet = require('pushbullet');

or

import {PushBullet} from 'pushbullet';

or

import {PushBullet} from '../node_modules/pushbullet/lib/pushbullet.js';

All without luck...
Please provide a working example with tsconfig.json and package.json included.

I am working on Windows 10, Node 14.0.3, npm 6.14.5.

Many thanks!

@alexwhitman
Copy link
Owner

I think you're almost there, the import just needs tweaking.

import PushBullet from 'pushbullet';

const pb = new PushBullet('API_KEY');

(async () => {
        try {
                let result = await pb.me();
                console.log(await result.json());
        }
        catch (e) {
                console.log(e.message);
        }
})();

@joekiller
Copy link

joekiller commented Feb 20, 2022

It took me about a day to get this running on typescript as well. I guess commonjs is on the way out for everything.

Minimal steps to make it work.

In package.json include "type": "module".
In tsconfig.json ensure under compilerOptions you have "module": "es6" or whatever ES target you want. It will not work with a commonjs module setting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants