Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How to import module from index.js

$
0
0

I created an example node.js project:

"name": "example","version": "1.0.0","type": "module",

Here is the index.js (two rows only):

"use strict";import MapLibreGlDirections, { LoadingIndicatorControl } from "@maplibre/maplibre-gl-directions";

Install the "@maplibre/maplibre-gl-directions" in global:

# npm install "@maplibre/maplibre-gl-directions" --global

Now, run the index.js with node:

# node index.js

Here is the error:

node:internal/errors:496    ErrorCaptureStackTrace(err);    ^Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@maplibre/maplibre-gl-directions' imported from /root/Documents/test/index.js    at new NodeError (node:internal/errors:405:5)    at packageResolve (node:internal/modules/esm/resolve:890:9)    at moduleResolve (node:internal/modules/esm/resolve:939:20)    at defaultResolve (node:internal/modules/esm/resolve:1132:11)    at nextResolve (node:internal/modules/esm/loader:163:28)    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)    at link (node:internal/modules/esm/module_job:76:36) {  code: 'ERR_MODULE_NOT_FOUND'}Node.js v18.18.2

I copied the "import" from here:https://github.com/maplibre/maplibre-gl-directions

The package "@maplibre/maplibre-gl-directions" has already been installed in global, but index.js still can't find the module (ERR_MODULE_NOT_FOUND).

What's wrong in the index.js? How to import that module?

Update:

After installing vite in global with npm, try to build with the following files and contents but it's not working.

package.json:

{"name": "example","version": "1.0.0","description": "","main": "index.js","type": "module","scripts": {"dev": "vite","build": "vite build","preview": "vite preview"  },"author": "","license": "ISC"}

index.js:

"use strict";import MapLibreGlDirections, { LoadingIndicatorControl } from "@maplibre/maplibre-gl-directions";console.log("hello world in console");function hello() {  document.write("hello world in document")}module.exports = hello;

index.html:

<html><head><script src="index.js"></script></head><body>  hello world in body!<script type="javascript">    new hello()</script></body></html>

After running:

# npm run build# npm run preview

Here is the output in Firefox:

hello world in body! 

Here is the expected output in Firefox:

hello world in body! hello world in document!

The vite is not building the "index.js" in the project. How to make this example project work with vite build?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>