My custom NPM Package is not found - node.js

Got very strange issues. Basically i decided create my own npm package, and publish it to the world. During development, I was testing it as s simple node module, and was able to use it using next code:
var r = require('./lib/%mymodulename%');
Of course it was in the lib folder.
Now, I organised it as a npm package, and my package.json looks next:
{
"name": "mymodulename",
"author": "xxx",
"description": "xxx",
"version": "0.0.1",
"homepage": "xxx",
"repository": {
"type": "git",
"url": "xxx"
},
"main": "/lib/mymodulename.js",
"scripts": {
"install":"node install.js"
},
"dependencies": {},
"engines": {
"node": ">=0.9"
}
}
when i am trying to test it via : npm install . -g it is installed successfully and i am able to see my local module via:
npm ls -g
however, when i am trying to use it in node file like:
var r = require('mymodulename') npm can't find it.
I think that i am missing something very small, but can't find what.
Thanks,
-D

Ok! Thanks for the answers.
It was totally my fault, and never put / for the main.
In my case i got :
"main": "/lib/mymodulename.js",
and it should be:
"main": "lib/mymodulename.js",
Thanks!

Related

"/" is not recognized as an internal or external command

Today, I'm coding a few npm packages and a few things that need to be prepared repeatedly.
So I wanted to code a CLI to get those things done quickly.
Here is the src/cli.js code:
export function cli(args){
console.log(args);
}
Here is the package.json code:
{
"name": "my-project",
"version": "1.0.0",
"description": "A CLI to bootstrap new project",
"main": "src/index.js",
"bin": {
"#kensoni/my-project": "bin/my-project",
"my-project": "bin/my-project"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"cli"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ken Nguyen",
"license": "MIT",
"dependencies": {
"arg": "^5.0.0",
"esm": "^3.2.25",
"inquirer": "^8.1.1"
}
}
Here is the bin/my-project code:
#!/usr/bin/env/ node
require = require('esm')(module /*, options*/);
require('../src/cli').cli(process.argv);
After I execute the command npm link and open a new cmd type my-project, I get the following message:
'"/"' is not recognized as an internal or external command,
operable program or batch file.
I am using these versions:
node: 14.17.1
npm: 7.18.1
Any ideas how it might work.
Thanks in advance.
Remove "/" after env
#!/usr/bin/env node
//...

npm link is not working, bash: dosmth: command not found

I am trying to do basic CLI with node, but seems like it is not linking from/to correct directory.
I have file named create, which contains command: console.log(process.cwd());
When I run in bash node create, it gives me that outcome:
/Users/katya/Desktop/code/drawer.
However, after running npm link (or sudo npm link), it prints that:
/Users/katya/.npm-packages/bin/dosmth -> /Users/katya/.npm-packages/lib/node_modules/drawer/create
/Users/katya/.npm-packages/lib/node_modules/drawer -> /Users/katya/Desktop/code/drawer
and after that if I run dosmth in bash I get:
bash: dosmth: command not found
I assume there is something to do with ./npm-packages/ appearing in the path.
I tried to delete node completely from computer and install again but did not help.
If you have any idea I would really appreciate your help.
My package.json:
{
"name": "drawer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"dosmth": "create"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Have you tried changing bin to point to your index.js?
{
// Other keys ...
"bin": {
"dosmth": "./index.js"
},
//...
}

Unable to run a node.js file with #babel/preset-env

I'm trying to run a simple node.js file which needs the #babel/preset-env preset. The moment I run the js file, I get a message saying
Requires Babel “7.0.0-0” but was loaded with “6.26.3”
To replicate the issue, please try the following in a new folder:
1. Run the following commands
npm init
npm install #babel/register
npm install #babel/core#^7.2.2
npm install #babel/preset-env
Create a .babelrc file with the following
{
"presets": ["#babel/preset-env"],
"plugins": []
}
Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');
Now run the parse.js file by running
node parse.js
You should see the error mentioned above. I have been trying to fix with for a long time now. Please help.
Many Thanks
followed your instructions and using #babel/register instead with
this package.json run with no issues
tasted on
node : v8.11.2
yarn : 1.12.3
paese.json
require('#babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)
packge.json
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/register": "^7.0.0"
},
"dependencies": {
"react": "^16.7.0"
}
}
Found the problem. The .babelrc file that contained a reference to #babel/preset-env. Removed it and the js file compiled just fine.

npm link not working on windows?

I'm using node v0.10.32. Windows 8.1.
My objective is to link a node application as a node_module in another main app.
I go to my-module folder and do
npm link
Then, I go to the main-app folder and do
npm link my-module
This is the result
c:\dev\main-app>npm link my-module
unbuild my-module#0.0.2
c:\dev\main-app\node_modules\my-module -> C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module -> C:\dev\my-module
But, the linkage does NOT seem to work, require('my-module') throws the following error
c:\dev\main-app>node app.js
module.js:340
throw err;
^
Error: Cannot find module 'my-module'
at Function.Module._resolveFilename (module.js:338:15)
my-module is indeed v0.0.2.
I can access it from main-app/node_module/my-module
This folder exists C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module
my-module package.json has "name": "my-module"
Moreover, %NODE_PATH% is correctly set:
c:\dev\main-app>echo %NODE_PATH%
C:\Users\Nizar\AppData\Roaming\npm\node_modules
Ideas?
There are a few things to try. On Windows, npm link is handled by creating junction points. Issuing a dir node_modules command should result in a line like:
01/15/2016 11:02 AM <JUNCTION> my-module [C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module]
Assuming that's there, then the problem is more than likely:
A lack of an index.js file (which is the default filename node uses to resolve modules)
You're using a different file than index.js as the main file of your module, in which case you need to tell node what that file is, by using the main key in your package.json file.
For example (taken from here):
{
"name": "node-js-sample",
"version": "0.2.0",
"description": "A sample Node.js app using Express 4",
"main": "index.js", // <-- LIKE THIS
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.13.3"
},
"engines": {
"node": "4.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-sample"
},
"keywords": [
"node",
"heroku",
"express"
],
"author": "Mark Pundsack",
"contributors": [
"Zeke Sikelianos <zeke#sikelianos.com> (http://zeke.sikelianos.com)"
],
"license": "MIT"
}

Node.js shrinkwrapped package.json causes npm install to update new versions regardless

I've got a npm-shrinkwrap.json and a package.json in a git branch called "deployment".
On my servers, I fetch and merge this deployment branch from github. This ensures that my servers have the latest deployment version.
Because the node_modules binaries etc. are not being shipped, I need to run npm install or npm update on the server side too, after the project repository has been pulled from the server.
This is why I decided to use npm shrinkwrap. However, even when I have this npm-shrinkwrap.json in the main folder and run npm install, it still installs newer versions of submodules, even though the shrinkwrapped json file has locked these down. It seems like npm does not even look at the shrinkwrap file.
Could anyone explain why this happens, and how to resolve this situation?
This is part of package.json:
"dependencies" : {
"eventemitter2" : "0.4.9",
"after" : "0.4.1",
"express" : "2.5.9"
},
"devDependencies" : {
"mocha" : ">= 1.0.3 < 2",
"should" : ">= 0.6.3 < 1",
"request" : ">= 2.9.202 < 3",
"commander" : ">= 0.6.0 < 1"
},
Whereas npm-shrinkwrap.json is:
{
"name": "appname",
"version": "0.0.1",
"dependencies": {
"eventemitter2": {
"version": "0.4.9"
},
"after": {
"version": "0.4.1"
},
"express": {
"version": "2.5.9",
"dependencies": {
"connect": {
"version": "1.8.7",
"dependencies": {
"formidable": {
"version": "1.0.9"
}
}
},
"mime": {
"version": "1.2.4"
},
"qs": {
"version": "0.4.2"
},
"mkdirp": {
"version": "0.3.0"
}
}
},
"commander": {
"version": "0.6.0"
},
"should": {
"version": "0.6.3"
},
"request": {
"version": "2.9.202"
}
}
}
Yet, when I ran npm install it updated qs from version 0.4.2 to version 0.5.0. Also, it updated mime to 1.2.5. Why did it do this?
The npm install returned this:
qs#0.5.0 ./node_modules/express/node_modules/connect/node_modules/qs
mime#1.2.5 ./node_modules/express/node_modules/connect/node_modules/mime
Interestingly enough the shrinkwrap contains neither of these. I guess this is the problem. Now the question is why it did not contain these.
Your npm-shrinkwrap.json doesn't include connect's version of qs. You should npm install before you generate your shrinkwrap.

Resources