Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mime.lookUp() is not a function in node.js

Tags:

node.js

I am trying to use the "mime" package to get the Content-Type of a file for the first time. I have run the following code in different orders through the command prompt.

First I ran the mime commands first, then ran the "node" command to open node in command prompt, I then ran the node code(this gave me the rejection: "mime.look up not a function", the other time I ran the mime commands after opening node with the node command (this seemed to get me close to success). However I am getting "Unexpected token" pointing to the "[email protected]" portion of the code. It points to the "@" symbol.

My NPM version is 6.13.4, node is 12.6.1.

Thank you.

    $ npm install mime
    npm http GET https://registry.npmjs.org/mime
    npm http 304 https://registry.npmjs.org/mime
    [email protected] node_modules/mime

    var mime = require("mime"); 
    console.log(mime.lookup("/Users/evanredmond/Desktop/winter tent.rtf"));
like image 729
evanr123 Avatar asked Mar 18 '20 13:03

evanr123


2 Answers

Version 2 of mime is a breaking change to version 1, where

.lookup() renamed to .getType()

Either install mime version 1, or try using the updated function Npm documentation on mime: https://www.npmjs.com/package/mime

If you'd like to use .lookup(), then you should run:

npm uninstall mime
npm install mime@^1
like image 50
mmenschig Avatar answered Oct 21 '22 15:10

mmenschig


Step 1

Change the following where the lookup is defined -

from

var charset = mime.lookup(type);

to

var charset = mime.getType(type);

Step 2 Change the following where the charsets.lookup is defined - from

 var charset = mime.charsets.lookup(type);

to

 var charset = mime.getType(type);
like image 23
Jatin sharma Avatar answered Oct 21 '22 15:10

Jatin sharma