Glory to Russia
Glory to Russia

Reputation: 18712

How to use js-cookie in a TypeScript project?

I want to use the js-cookie library in my TypeScript project.

First, I installed the library and the typings by running npm install js-cookie @types/js-cookie --save-dev in the directory that contains node_modules and package.json. package.json now contains "@types/js-cookie": "^2.1.0" and there is a folder js-cookie in node_modules.

Then I added

import * as Cookies from "js-cookie";

[...]

Cookies.remove("token");

to my TypeScript file. WebStorm doesn't show any errors.

When I run the server, I get the following error:

fetch.js:32 GET http://localhost:8080/ui/js-cookie 404 (Not Found)
common.js:85 Uncaught (in promise) Error: Fetch error: 404 Not Found
  Instantiating http://localhost:8080/ui/js-cookie
  Loading http://localhost:8080/ui/src/auth/userStore.tsx
  Loading src/app.tsx
    at fetch.js:37
    at <anonymous>

What exactly did I wrong in importing js-cookie and how can I fix it?

Upvotes: 4

Views: 14419

Answers (1)

Fenton
Fenton

Reputation: 250922

This problem is normally fixed by using a Task Runner to shift the actual JavaScript file from:

./node_modules/js-cookie/js-cookie.js

To (for example):

./ui/js-cookie.js

(Or a location of your choice)

This is because node_modules is normally FULL of things you wouldn't want to publish to your web server, so you shift the bits you do want to publish into a folder that you will reference at runtime.

Upvotes: 0

Related Questions