Home > Net >  cannot import npm installed modules (lightweight-charts)
cannot import npm installed modules (lightweight-charts)

Time:04-24

I'm new to the world of JS and recently trying to write a toy app using lightweight-charts. I've gone through several tutorials and learned some basic concepts. But I'm so mad at myself that I still cannot figure out in what context can I use the 'es6 via npm' example mentioned in the lightweight-chart NPM page above. The CDN example seems to be fine, though.

Here is what I did

  1. install lightweight-charts locally. - npm install lightweight-charts

  2. create a test.mjs in the same directory, which contains only one lines.

import { createChart } from 'lightweight-charts';
  1. run the code. node test.mjs

But I got the following errors

SyntaxError: Named export 'createChart' not found. The requested module 'lightweight-charts' is a CommonJS module, which may not support all module.exports as named exports.

It does not make sense for me because the official npm site says es6 supported, but the error messages seem to indicate that this module is for CommonJS. Even after I
rewrote this in CommonJS style using 'require' syntax and rename "test.mjs" to "test.js", some dependency modules are still using 'import' syntax and caused some so
rt of "cannot use import statement outside a module" errors.

CodePudding user response:

This package is not meant to be executed on the backend with NodeJS. It will only work in the browser.

CodePudding user response:

Maybe you need to install Babel compiler here

  • Related