chart.js-node-ssr-example icon indicating copy to clipboard operation
chart.js-node-ssr-example copied to clipboard

Run in node without webpack ?

Open cameronbraid opened this issue 5 years ago • 4 comments

Is there any way to run this in node without using webpack ?

cameronbraid avatar Jan 12 '20 14:01 cameronbraid

If you use Node>=13.0.0, webpack is reqired.
On 10<=Node<13, it is possible to run w/o webpack. (but not recommended)

  • You should write the build script using tsc, and change some import statements.
  • Output file's extension should be *.mjs.
  • Node CLI options is needed.

index.ts

import { SvgCanvas,
         SvgCanvas2DGradient } from 'red-agate-svg-canvas/modules';

// NOTE: hack bad .d.ts definition for ESM.
import * as ChartJs_ from 'chart.js';
const ChartJs: typeof ChartJs_ = (ChartJs_ as any).default || ChartJs_;

See also: https://github.com/shellyln/mdne/blob/12f7e4d9a54d991f55390cdf0d8c8f806fad4e60/package.json#L49-L50 https://github.com/shellyln/menneu/blob/master/src/components/Chart.tsx https://github.com/shellyln/menneu/blob/master/package.json

shellyln avatar Jan 12 '20 15:01 shellyln

I found an easy way - install esm and ts-node

https://github.com/cameronbraid/chart.js-node-ssr-example/commit/f987196d0eabe00ee13c10811f8b45394d768071

to see it working, checkout my branch :

npm install
npm run run

cameronbraid avatar Jan 13 '20 01:01 cameronbraid

Hi, @cameronbraid .
Thanks for your useful information.
I think it's a very good way to get started this example w/o webpack.

shellyln avatar Jan 13 '20 02:01 shellyln

The latest commit can now be run without Webpack. b9da40b526b30ca71cae1239bf160ca545b02da9

npm run build
npm run start:v12

Node>=12.13.0 is required.

shellyln avatar Feb 03 '20 15:02 shellyln