Mehan Alavi
Mehan Alavi

Reputation: 327

How to import a npm package

I have published a npm package called sum . which have just main.js file:

export function sum (a , b){
    return a+b
}

My package.json is like this :

{
"name":"sum",
"version":"1.0.0",
"description":"simple npm package",
"main":"./main.js",
"module":"./main.js"
}

I installed it in another directory and import it:

import {sum} from "sum"
alert(sum(1,2))

but it gave this error:

Uncaught TypeError: Failed to resolve module specifier "sum". Relative references must start with either "/", "./", or "../".

Upvotes: 0

Views: 2314

Answers (1)

ME-ON1
ME-ON1

Reputation: 83

locally you need to spacify the path of the module main.js file on importing

like this

import {sum} from '/path/to/direc/main.js'

or

you can add the package in your local npm modules and test.

for this you have to run.

cd ~/projects/your-pkg      # go into the package directory
npm link                    # creates global link
cd ~/projects/curr_proj     # go into some other package directory.
npm link package name       # link-install the package

Upvotes: 1

Related Questions