Create npm package using webpack - node.js

I'm creating an npm package and using webpack for loader like babel, eslint etc.. However I'm under assumption that final compiled version of the package should only contain that one module, without webpackBootstrap.
My current package, webpack config and source. I stripped it down to just make it "work".
Steps I took to check if it's working:
npm install
npm run build
npm install -g .
node
var test = require('test-package');
Resulting in this error:
Error: Cannot find module 'test-package'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at repl:1:12
at REPLServer.defaultEval (repl.js:248:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:412:12)
at emitOne (events.js:82:20)
I'm new to webpack and npm so let me know if you need any more information.

Set output.libraryTarget to umd. That will give you something that's easy to consume from various module systems (global, AMD, CommonJS).
output.library is another useful field to set. That should match the name of your library global you want.
There was another problem beyond this. To make the import work npm link needed be used. This feature is highly useful during development. You can revert a link through npm unlink.

Related

npm run gulp primordials not defined

I'm trying to run npm run gulp and I'm coming up with this primordials is not defined error (I have also added the npm-shrinkwrap.json file as suggested which gets updated after running npm install but it doesn't fix my primordials is not define error):
> gulp-tutorial#1.0.0 gulp
> gulp
fs.js:47
} = primordials;
^
ReferenceError: primordials is not defined
at fs.js:47:5
at req_ (/Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main/node_modules/natives/index.js:143:24)
at Object.req [as require] (/Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main/node_modules/natives/index.js:55:10)
at Object.<anonymous> (/Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main/node_modules/vinyl-fs/node_modules/graceful-fs/fs.js:1:37)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
Node.js v17.0.1
Any suggestions 🙌 ?
Primordials are used by Node.js internal modules and are not accessible to end user scripts. (That's their entire point for existing. Otherwise, you could modify Object.prototype and end up affecting Node.js internals, causing weird crashes and things like that.)
The module that is giving you this error is the natives module. The page for the package says:
This module relies on Node.js's internals and will break at some point. Do not use it, and update to graceful-fs#4.x.
Sounds like it's at that point where it's broken. Use npm ls natives to find out what's using it. (You might need to be in the /Applications/MAMP/htdocs/wordpress/wp-content/themes/theming-main directory when you run the command.) From there, you can try to figure out how to get rid of it.
Another possibility would be to try downgrading to an earlier version of Node.js to see if it works with that version.
The problem was with the versions not matching up. I also had 2 NodeJS installation paths and that was throwing things off.
I uninstalled them all, started from the beginning and used brew to reinstall.
Then checked that the versions were all compatible.
I also had to install node-sass and referred to this compatibility table to get the correct version - https://github.com/sass/node-sass#node-version-support-policy
Now its working and I can get on developing :) As of today Im running Node v17.0.1 Npm v8.1.0 Gulp cli v2.3.0

Cannot find Realm module "(...)node_modules\Realm\build\Release\realm.node"

I'm trying to create node server using Realm. I have installed Realm like the docs say:
npm install realm. The module is visible in node_modules folder and package.json.
I try to import Realm like the docs say too:
const Realm = require("realm");
But this import throws an error (it's in my language, so translation of the error message might be not word-to-word):
Error: cannot find module:
\\?\D:\GIT\nuxt-test\node_modules\realm\build\Release\realm.node
at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at bindings (D:\GIT\nuxt-test\node_modules\bindings\bindings.js:112:48)
at getRealmConstructor (D:\GIT\nuxt-test\node_modules\realm\lib\index.js:37:43)
at Object.<anonymous> (D:\GIT\nuxt-test\node_modules\realm\lib\index.js:62:26)
at Module._compile (internal/modules/cjs/loader.js:701:30)
Additional info:
OS - Windows 7
Node version: 16.4 (upgraded from 10.15.3 trying to solve this)
What might be wrong with my import?
EDIT:
I have noticed that on installation of Realm npm throws some warnings:
realm#10.5.0 requires a peer of react-native#>=0.60 but none is installed.
deprecated-react-native-listview#0.0.6 requires a peer of react#* but none is installed.
which is surprising as I'm not using react-native SDK, but the node one.
I tried to install realm-web package and it seems to function properly (!) even though I run it from node. But I don't know if this is an acceptable solution as docs say the realm-web package is not intended for node.

Using nodejs bindings to run native c++ code produces an error

I'm trying to develop an electron app, and I would like to use an npm package that gives vibrancy to a window. This is the package I'm trying to use: https://github.com/sebascontre/windows10-fluently-vibrancy
It uses a visual c++ code to call native windows methods to achieve this vibrancy effect.
I've cloned and I've run the npm install and build as described in the readme and ran the following:
npm install
npm run conf
npm run rebuild
All built successfully and I also see the build\debug artifacts of the c++ build.
When I try to run it (as described in readme):
cd spec\app
electron .
I'm getting the following error message:
Uncaught Exception:
Error: The specified module could not be found.
\\?\F:\Development\windows10-fluently-vibrancy\build\Debug\Vibrancy.node
at process.func [as dlopen] (electron/js2c/asar.js:155:31)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:857:18)
at Object.func [as .node] (electron/js2c/asar.js:155:31)
at Module.load (internal/modules/cjs/loader.js:677:32)
at tryModuleLoad (internal/modules/cjs/loader.js:609:12)
at Function.Module._load (internal/modules/cjs/loader.js:601:3)
at Module.require (internal/modules/cjs/loader.js:715:19)
at require (internal/modules/cjs/helpers.js:14:16)
at bindings (F:\Development\windows10-fluently-vibrancy\node_modules\bindings\bindings.js:112:48)
at Object.<anonymous> (F:\Development\windows10-fluently-vibrancy\index.js:1:35)
According to the message it cannot find the Vibrancy.node file - But I've verified that it exists in the specified location.
Any ideas how to make it run? I have cloned the package to test it since when trying to use the npm itself in my project it gave the same error so I figured I would try the source.
Today I had the exact same issue you're describing here.
I got it to work using electron-rebuild.
From the root of your project run the command: electron-rebuild
Hope this helps you, if not, maybe someone else.

How to add node require libraries to path?

After installing bitcoinjs and firing up node and running :
var bitcoin = require('bitcoinjs-lib');
I receive error :
Error: Cannot find module 'bitcoinjs-lib'
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:15
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:431:12)
at emitOne (events.js:82:20)
When I run these commands it appears to find the library :
$ pwd
/usr/local/lib/node_modules
$ ls
bitcoinjs-lib browserify npm uglify-js
$ node
> var bitcoin = require('bitcoinjs-lib');
undefined
I think it works in this case because I'm running node from library /usr/local/lib/node_modules
How can I add the modules library to node so can find a required library from any location instead of having to change directory into /usr/local/lib/node_modules?
I've tried modifying path in bash_profile to :
PATH=$PATH:/usr/local/lib/node_modules
But this does not fix issue.
To get this working the quick way, you need to set $NODE_PATH:
NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules
However, in general, Node modules shouldn't be installed globally (which means in a "system directory" like /usr/local/lib/node_modules).
Instead, you create a project directory, and install the module there:
$ mkdir my-project
$ cd my-project
$ npm install bitcoinjs-lib
If you run Node in that project directory, require('bitcoinjs-lib') will work.
A hacky way:
module.paths.unshift(<path>);
process.env.NODE_PATH = <path>
module.constructor._initPaths();
I figure out the above solution when I wrote a Cocos Creator(based on Electron) plugin which is loaded during the IDE opening. And I need to require a utils script from that plugin which is not located in any require paths. I tested the first code snippet in CC and Node.js v10.16.0 for Win10.

Why isn't the underscore module available in the Node.js console?

I ran the following code to install the underscore js module:
npm install -g underscore
I then tried to access it via the node console, but I get the following error:
node
> __ = require('underscore');
Error: Cannot find module 'underscore'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at repl:1:6
at REPLServer.self.eval (repl.js:109:21)
at rli.on.self.bufferedCmd (repl.js:258:20)
at REPLServer.self.eval (repl.js:116:5)
at Interface.<anonymous> (repl.js:248:12)
at Interface.EventEmitter.emit (events.js:96:17)
Why doesn't this example work?
I don't really know why, but it fails indeed (when installing underscore globally, as you have done).
If you install it without -g, it should work (be careful, however, as '_' is already used by Node REPL to hold the result of the last operation, as explained here:
Using the Underscore module with Node.js
Do you really need to install it globally?
I just had the same problem
$ export NODE_PATH=/usr/local/share/npm/lib/node_modules
sorted it out for me; this obviously depends on your platform and where npm has installed it. Also, as mentioned in Javo's answer, don't name it _ in the REPL.

Resources