Can I import a library installed with npm into a web worker?
I need to use the moment.js
library into a web worker.
It is installed via npm
into the node_modules/moment
directory
I already have tried with this at the top of the worker.js file:
importScripts('/node_modules/moment/moment.js');
But I get
GET http://192.168.2.1:8100/node_modules/moment/moment.js 404 (Not Found)
Yes, it's possible, chances are you're already using a popular bundler like webpack
or parcel
, but even if you're not it's still possible, though probably not directly from node_modules
// relative path to the worker from current file
const worker = new Worker('../utils/myWorker.js');
// use import like you would in any other file
import moment from 'moment';
console.log(`From worker: worker started at ${moment().format('HH:mm:ss')}`);
// relative to the expected public path (have in mind any filename transforms like hashing)
const worker = new Worker('myWorker.bundle.js');
// use import like you would in any other file
import moment from 'moment';
console.log(`From worker: worker started at ${moment().format('HH:mm:ss')}`);
{
entry: {
main: './src/app/main.js'
worker: './src/utils/myWorker.js',
},
output: {
path: `${ROOT_PATH}/public`,
filename: '[name].bundle.js',
}
}
Install the loader
npm install worker-loader --save-dev
worker-loader
documentation https://www.npmjs.com/package/worker-loader
// relative path to the worker from current file
import Worker from '../utils/myWorker.worker.js';
const worker = new Worker();
// use import like you would in any other file
import moment from 'moment';
console.log(`From worker: worker started at ${moment().format('HH:mm:ss')}`);
{
module: {
rules: [
{
test: /\.worker\.js$/,
use: { loader: 'worker-loader' }
}
]
}
}
Using an inline loader
npm install worker-loader --save-dev
worker-loader
documentation https://www.npmjs.com/package/worker-loader
/* eslint-disable import/no-webpack-loader-syntax */
import Worker from "worker-loader!./myWorker.worker.js";
const worker = new Worker();
// use import like you would in any other file
import moment from 'moment';
console.log(`From worker: worker started at ${moment().format('HH:mm:ss')}`);
importScripts('//cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js');
console.log(`From worker: worker started at ${moment().format('HH:mm:ss')}`);
importScripts
from parent folders for security reasonsnode_modules
folder is usually at the root of the project so you can't access it with importScripts
For projects using webpack as a bundler the 2 webpack solutions can be adapted as long as you can access the webpack config and customize it. If you can't access the config (CRA) you can use the inline loader from the "With CRA" example
The "Webpack (as entry)" example was actually borrowed from Angular CLI generated app with Web Workers
{
"extends": "../generic-tsconfig.json",
"compilerOptions": {
"lib": ["esnext", "webworker"],
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With