1

I'm building a npm package that I want to publish. I followed this instructions and works untill I get to the require part. I've installed the package with npm install . -g and I can see it listed when I do npm ls -g. But when I require it I get

var VuePrint = require('vue-print')
Error: Cannot find module 'vue-print'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at repl:1:16
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)

Thats the error from node-repl, but I got the same in different environment with different stacktrace. So, How can I require my package to publish it later? Here are the relevant parts of my package.json file in case it helps

{
  "name": "vue-print",
  "description": "Vue plugin to easy print in the web",
  "author": "YerkoPalma <yerko.palma@usach.cl>",
  "version": "0.1.3",
  "main": "dist/vueprint.js",
  "files": [
    "dist/vueprint.js",
    "src",
    "README.md"
  ],
  "scripts": {
    "build": "cross-env NODE_ENV=production browserify -e src/vueprint.js | uglifyjs -c warnings=false -m > dist/vueprint.js",
    ...
  },
  "browserify": {
    "transform": [
      "vueify",
      "babelify"
    ]
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/YerkoPalma/vue-print.git"
  },
  "bugs": {
    "url": "https://github.com/YerkoPalma/vue-print/issues"
  },
  "dependencies": {
    "vue": "^1.0.0"
  },
  "devDependencies": {
    ...
  }
}
Yerko Palma
  • 12,041
  • 5
  • 33
  • 57
  • You've installed the package globally, but not locally by the looks of things. `require` doesn't check the folder where global modules are installed. As the docs say, if you `npm install ../my-package` then it will be installed into the node_modules folder and you should be able to use `require` – dan Apr 13 '16 at 16:11
  • I can require any other globally installed package. Also, I've tried before with the package locally installed – Yerko Palma Apr 13 '16 at 16:59
  • Assuming [this](https://www.npmjs.com/package/vue-print) is your module it works for me using `npm install vue-print`. The module should be in your `node_modules` folder though. See http://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package regarding using `require` on global modules. – dan Apr 13 '16 at 17:30
  • yes, that is, the install is working fine, but when requiring it it can't find it – Yerko Palma Apr 13 '16 at 17:31
  • Does it work if you install it locally rather than globally? – dan Apr 13 '16 at 17:32
  • no, it works if I directly require the source file `require('./src/vueprint.js')`. But when I require the module from the `npm install` (globally or locally) it doesn't work. I think that I have some misunderstanding about the config on my package.json, I guess that it should take the file that I added in the `main` option, but I tried with the source and compiled files (I'm using browserify) with no success – Yerko Palma Apr 13 '16 at 17:36
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109073/discussion-between-dan75-and-yerko-palma). – dan Apr 13 '16 at 17:38

1 Answers1

1

You don't need to install you package as a global package (-g). Just go to your project folder (where you want use vue-print) and run command "npm i vue-print", after that you can use require('vue-print') in this folder

sergiy.dragunov
  • 780
  • 6
  • 9
  • That require the packaged to be published. I didn't mention it but I already publish the package and it was successfully published, but again, not required. Globally or locally it makes no difference – Yerko Palma Apr 13 '16 at 17:13