user1249791
user1249791

Reputation: 1271

include js script from a npm install

I have a server where I cannot run npm, lack of permissions. Just Apache.

I need a JS library (ie: https://github.com/stevage/mapbox-gl-utils) that is deployed using npm. There is no 'old-style' JS script in which there is all I need (and that I can reference to in my html)

I used npm localy (own laptop, where I have permissions) and now I have a bunch of inter-dependent folders, with JS and json files, that as far as I know, only work into a npm server instance.

No JS including all necessary stuff has been created.

What do I have to do to make work the JS I need into my standard html (served on Apache)? or I am forced to use npm?

Upvotes: 1

Views: 228

Answers (1)

Olian04
Olian04

Reputation: 6872

You can pull down the npm dependency on a different computer, bundle it as a single JS file using any bundler of choice (for example rollup). Then move that bundled file to the server and use it as you would with any JS file you've written your self.

First install dependencies:

$ npm i mapbox-gl-utils rollup rollup-plugin-node-resolve rollup-plugin-commonjs

Then setup rollup:

// rollup.config.js
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
  input: './node_modules/mapbox-gl-utils/src/index.js',
  output: {
    file: './mapbox-gl-utils.js',
    format: 'iife',
    name: 'mapbox_gl_utils'
  },
  plugins: [
      resolve(),
      commonjs(),
  ]
}

Finally run rollup to bundle your dependency:

$ rollup --config

Upvotes: 2

Related Questions