Problems with 'fs' when I try to start snowpack - node.js

[15:03:25] [snowpack] Welcome to Snowpack! Because this is your first
time running this project, Snowpack needs to prepare your
dependencies. This is a one-time step and the results will be cached
for the lifetime of your project. Please wait... [15:03:25] [snowpack]
Package "fs" not found. Have you installed it?
When I try to start snowpack I got the error above...
Is 'fs' not include in node by default? When I tried to install 'fs' via npm install fs, not started as well. Some of my files(test files) use 'fs' to run some tests...
Obs: I am using to import:
import fs from 'fs';//(not require('fs'))
Thanks. André

I discovered how to deal with it in the snowpack website using the configuration in snowpack.config.js (packageOptions.external: ["fs", "path"])
module.exports = {
packageOptions: {
/* ... */
external: ["fs", "path"]
}
}

Related

Nodejs: Cannot find module `nanoid`

When I try to compile my Typescript project (NestJS) I came across the following problem.
Some packages like nanoid, cookie-parser give error Error: Cannot find module 'nanoid'
The code where they used is below:
...
import { nanoid } from "nanoid";
...
const confirmToken = nanoid(32);
...
Here are what I tried:
I checked the node_modules directory. The package is there
Tried to completely remove node_modules and package.json.lock.
Used v16.14.0, v17 versions of NodeJS
Used yarn and npm
None of these ways helped.
Again, this error happens with only certain packages.
What could be an issue?

Error importing 'vscode-languageserver' in a React app

I am trying to create a playground using monaco-editor and vscode-languageserver to show the features of my language server.
However when I try to import 'vscode-languageserver' from a page like the following example
//# src/pages/test.js
const { TextDocument } = require("vscode-languageserver");
console.log(TextDocument.create('a' , 'b' , 'c' , 'd' ));
https://github.com/zkrami/docusaurus-test/blob/master/src/pages/test.js
I get the following errors:
Module not found: Can't resolve 'child_process' in 'C:\Users\Rami\git\my-website\node_modules\vscode-languageserver\lib'./node_modules/vscode-languageserver/lib/files.js
Module not found: Can't resolve 'fs' in 'C:\Users\Rami\git\my-website\node_modules\vscode-languageserver\lib'./node_modules/vscode-jsonrpc/lib/pipeSupport.js
and please note that if I imported the module in the docusaurus.config.js file it works perfectly.
I made a quick example you can try:
https://github.com/zkrami/docusaurus-test/
Specifications:
yarn 1.22.4
node v10.15.3
OS: Windows
#docusaurus/core: "^2.0.0-alpha.54"
I ended up using vscode-languageserver-protocol package which fulfill my requirements.
fs is a Node module and requires Node runtime, you can't use it in the browser.
This is not a Docusaurus issue, you wouldn't be able to use it in any client-side React app.

SyntaxError: The requested module 'graphql-relay' does not provide an export named 'fromGlobalId' [duplicate]

I've installed Node 8.9.1 (same problem happens in v10.5.0).
I'm trying to use named imports from npm packages in a file with the .mjs
import { throttle } from lodash;
I run:
node --experimental-modules index.mjs
and I get:
SyntaxError: The requested module 'lodash' does not provide an export named 'throttle'
at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)
--experimental-modules are supposed to stop being experimental in v10 LTS, so why haven't more module authors jumped on the bandwagon?
EDITED NEW (AND MUCH BETTER) ANSWER
The Node team is ... slow. Meanwhile, the same guy who brought us Lodash (John-David Dalton) imagined a brilliant solution, and his idea is the best way to get full ES6 module support in 2019.
(In fact, I want to delete my earlier answer, but I've left it for historical purposes.)
The new solution is SUPER simple.
Step #1:
npm i esm
(https://www.npmjs.com/package/esm for package details)
Step #2:
node -r esm yourApp.js
That's the entirety of it: it's really just that easy. Just add -r esm as a Node arg, and everything just magically works (it's even less typing than --experimental-modules!) Thank you John-David Dalton!!!
As I said in my original answer, presumably someday Node will finally release full ES6 support, but when that happens adopting it will be as easy as removing "-r esm" from a few scripts :D
Finally, to give credit where due, while I didn't find it through his answer, #Divyanshu Rawat actually provided an answer with the precursor to this library long before I made this update.
ORIGINAL ANSWER
--experimental-modules does not have support for named exports yet:
--experimental-modules doesn't support importing named exports from a commonjs module (except node's own built-ins).
https://github.com/apollographql/graphql-tools/issues/913
This is why you are unable to use the syntax:
import { throttle } from 'lodash';
Instead (for now at least) you have to destruct what you need:
import lodash from 'lodash';
const { throttle } = lodash;
Presumably someday Node will add support for all of the ES Module features.
I just had this error with nodejs express *.mjs file and --experimental-modules flag enabled for googleapis.
import { google } from "googleapis";
SyntaxError: The requested module 'googleapis' does not provide an export named 'google'
Solution
//not working!
//import { google } from "googleapis";
//working
import googleapis from "googleapis";
const { google } = googleapis;
I do not understand why this is the case; if anyone knows why, please comment.
You have to use .mjs extension.
Once this has been set, files ending with .mjs will be able to be loaded as ES Modules.
reference: https://nodejs.org/api/esm.html
Update:
Looks like you haven't export the method yet.
Suppose i have hello.mjs with content
export function sayHello() {
console.log('hello')
}
i can use it in index.mjs like this
import {sayHello} from './hello.mjs'
sayHello()
For me loading lodash as ES Library did the job, here is the NPM Package for the same.
The Lodash library exported as ES modules.
https://www.npmjs.com/package/lodash-es
Then you can import utils in normal way.
import { shuffle } from 'lodash-es';
If lodash had been written as modules, and lodash/index.mjs exported throttle: export const throttle = ...;, then you'd be able to import { throttle } from lodash;
The problem here is that in commonjs there's no such thing as a named export. Which means that in commonjs modules export one thing only.
So think that lodash exports an object containing a property named throttle.
For the second part of the question, I believe people will slowly start adopting ES Modules once it's not experimental anymore. At the time of this writing, it still is (Node.js v11.14).
#machineghost answer works. I remember also adding 'type':'module' to package.json along with using esm with node v12(LTS) and it worked fine.## Heading ##
I updated my node to v14(current) and I got an error
C:\Users\andey\Documents\Project\src\app.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
C:\Users\andey\Documents\Project\src\app.js
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1217:13) {
code: 'ERR_REQUIRE_ESM'
}
To fix it I had to remove 'type':'module' from package.json.
source

Getting error Cannot find module 'crypto'

I am trying to use node Crypto module in Angular 7 for asymmetric encryption.
and used below command to import the Crypto module
import * as crypto from 'crypto';
but still I am getting error that is
`ERROR in src/app/log-in/log-in.component.ts(11,25): error TS2307: Cannot find module 'crypto'.`
Please help me to resolve the error that how to use this library into Angular.
Thanks in Advance.
I was trying to import { randomBytes } from "crypto"; then such error occurred,
I installed node types npm install #types/node --save-dev and it was resolved.
Per the author on npm , The crypto package is no longer available as it is now built in to Node.js. I would suggest looking for an alternative. I came across a Github Gist that contains some suggestions: https://gist.github.com/jo/8619441
Make sure whatever you pick is useable in the browser. Some of the options listed there are server-side only.
Make sure that you install 'crypto' module from npm
use: npm i crypto to install this module.
for more information please visit here.
If this is still not working then you have to check for alternative module because this module is dedicated you can check this

node --experimental-modules, requested module does not provide an export named

I've installed Node 8.9.1 (same problem happens in v10.5.0).
I'm trying to use named imports from npm packages in a file with the .mjs
import { throttle } from lodash;
I run:
node --experimental-modules index.mjs
and I get:
SyntaxError: The requested module 'lodash' does not provide an export named 'throttle'
at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)
--experimental-modules are supposed to stop being experimental in v10 LTS, so why haven't more module authors jumped on the bandwagon?
EDITED NEW (AND MUCH BETTER) ANSWER
The Node team is ... slow. Meanwhile, the same guy who brought us Lodash (John-David Dalton) imagined a brilliant solution, and his idea is the best way to get full ES6 module support in 2019.
(In fact, I want to delete my earlier answer, but I've left it for historical purposes.)
The new solution is SUPER simple.
Step #1:
npm i esm
(https://www.npmjs.com/package/esm for package details)
Step #2:
node -r esm yourApp.js
That's the entirety of it: it's really just that easy. Just add -r esm as a Node arg, and everything just magically works (it's even less typing than --experimental-modules!) Thank you John-David Dalton!!!
As I said in my original answer, presumably someday Node will finally release full ES6 support, but when that happens adopting it will be as easy as removing "-r esm" from a few scripts :D
Finally, to give credit where due, while I didn't find it through his answer, #Divyanshu Rawat actually provided an answer with the precursor to this library long before I made this update.
ORIGINAL ANSWER
--experimental-modules does not have support for named exports yet:
--experimental-modules doesn't support importing named exports from a commonjs module (except node's own built-ins).
https://github.com/apollographql/graphql-tools/issues/913
This is why you are unable to use the syntax:
import { throttle } from 'lodash';
Instead (for now at least) you have to destruct what you need:
import lodash from 'lodash';
const { throttle } = lodash;
Presumably someday Node will add support for all of the ES Module features.
I just had this error with nodejs express *.mjs file and --experimental-modules flag enabled for googleapis.
import { google } from "googleapis";
SyntaxError: The requested module 'googleapis' does not provide an export named 'google'
Solution
//not working!
//import { google } from "googleapis";
//working
import googleapis from "googleapis";
const { google } = googleapis;
I do not understand why this is the case; if anyone knows why, please comment.
You have to use .mjs extension.
Once this has been set, files ending with .mjs will be able to be loaded as ES Modules.
reference: https://nodejs.org/api/esm.html
Update:
Looks like you haven't export the method yet.
Suppose i have hello.mjs with content
export function sayHello() {
console.log('hello')
}
i can use it in index.mjs like this
import {sayHello} from './hello.mjs'
sayHello()
For me loading lodash as ES Library did the job, here is the NPM Package for the same.
The Lodash library exported as ES modules.
https://www.npmjs.com/package/lodash-es
Then you can import utils in normal way.
import { shuffle } from 'lodash-es';
If lodash had been written as modules, and lodash/index.mjs exported throttle: export const throttle = ...;, then you'd be able to import { throttle } from lodash;
The problem here is that in commonjs there's no such thing as a named export. Which means that in commonjs modules export one thing only.
So think that lodash exports an object containing a property named throttle.
For the second part of the question, I believe people will slowly start adopting ES Modules once it's not experimental anymore. At the time of this writing, it still is (Node.js v11.14).
#machineghost answer works. I remember also adding 'type':'module' to package.json along with using esm with node v12(LTS) and it worked fine.## Heading ##
I updated my node to v14(current) and I got an error
C:\Users\andey\Documents\Project\src\app.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
C:\Users\andey\Documents\Project\src\app.js
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1217:13) {
code: 'ERR_REQUIRE_ESM'
}
To fix it I had to remove 'type':'module' from package.json.
source

Resources