4

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?

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.