question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please add {"type": "module"} to package.json

See original GitHub issue

You are publishing a module but not declaring it as such in package.json which makes it impossible to use

-> % cat test.mjs 
import {} from "ip-address";

-> % node test.mjs
(node:71803) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
./node_modules/ip-address/dist/esm/ip-address.js:1
import { Address4 } from './lib/ipv4';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)

Node.js v18.1.0

Currently to use this module we have to explicitly import as a CommonJS module:

import { createRequire } from "module";
const { Address4, Address6 } = createRequire(import.meta.url)("ip-address") as typeof import("ip-address");

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
laverdetcommented, Jul 20, 2022

I have the updated module published to npm as @laverdet/beaugunderson-ip-address. Our CI/CD conventions forbids git references so I put up there instead.

0reactions
maxpaincommented, Aug 15, 2022

Up

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is 'type: module' in package.json file? - Stack Overflow
When you have "type": "module" in the package.json file, your source code should use import syntax. When you do not have, you should...
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
Within a package, the package.json "type" field defines how Node.js should interpret .js files. If a package.json file does ...
Read more >
Add type = "module" in package.json to support nodejs in ...
To make it work, I had to add a main entry to the package.json as well as the "type": "module" (otherwise I just...
Read more >
"module" in the package.json or use the .mjs extension 2022
Set type property in Package.json to module ... Now you can open the package.json file then add a new property called type and...
Read more >
Creating Node.js modules - npm Docs
To create a package.json file, on the command line, in the root directory of your Node.js module, run npm init : · Provide...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found