在我的package.json文件中,我指定了我的nodejs应用程序的类型module,因为如果我不这样做,似乎我不能使用import语句。这是现在的样子:
{
"name": "...",
"version": "1.0.0",
"description": "....",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "...."
},
"author": "",
"license": "ISC",
"devDependencies": {
"lodash": "^4.17.15"
},
"type": "module"
}
Run Code Online (Sandbox Code Playgroud)
但是如果我将"type": "module"加到我的package.json文件中,我就不能再使用require语句了,因为我得到了一个ReferenceError: require is not defined错误。
如果我"type": "module"从package.json 中删除该行,并将所有导入重写为 requires,则一切正常,没有错误。
我似乎无法找到任何迹象,这import与require在同一个脚本不能混用或一起使用,我失去了一些东西在这里,或我有一些其它的错误?我怎样才能决定在同一个脚本中使用这两种语句?
为什么我需要这个,是因为我想要 …
我正在尝试从 node_modules 转译一个模块“react-device-detect”,但还无法实现。以下是我尝试过的:
module.exports = withLess({
webpack: (config, { isServer, defaultLoaders }) => {
// other configs
config.module.rules.push({
test: /\\.+(ts|tsx)$/,
include: [path.join(__dirname, 'node_modules/react-device-detect')],
// exclude: /node_modules/,
use: [{ loader: 'ts-loader' }],
});
config.module.rules.push({
test: /\\.+(js|jsx)$/,
include: [path.join(__dirname, 'node_modules/react-device-detect')],
// exclude: /node_modules/,
use: [{ loader: 'babel-loader' }],
});
return config;
},
});
Run Code Online (Sandbox Code Playgroud)
我也单独尝试过这些规则,但没有奏效。
更新 1: 完成 next.config.js 配置 @felixmosh
const webpack = require("webpack");
const withLess = require("@zeit/next-less");
const { parsed: localEnv } = require("dotenv").config();
require("dotenv").config({
path: process.env.NODE_ENV === "production" ? ".env.production" …Run Code Online (Sandbox Code Playgroud) 我试图运行一个简单的代码块,以 React "render" method 为特色,但浏览器没有显示文本。
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(<h1>Hello World</h1>,document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
我使用 VS Code 作为编辑器,因此我输入了“运行和调试 Node.js”命令。它提出了以下警告
(node:3004) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Run Code Online (Sandbox Code Playgroud)
在 package.json 文件中设置“type:module”解决了问题,但另一方面出现了另一个问题
(node:18968) ExperimentalWarning: The ESM module loader is experimental.
warning.js:32
SyntaxError: Unexpected token '<'
at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
at async link (internal/modules/esm/module_job.js:37:21)
Run Code Online (Sandbox Code Playgroud)
那不允许我写任何标签。我错过了什么,我该如何解决?下面是 index.html 文件和文件结构
<html>
<head>
<title> React Test</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
拜托,我开始学习npm。如果我想要安装“@fullcalendar”并使用它,我会收到错误:“未捕获的类型错误:无法解析模块说明符“tslib”。相对引用必须以“/”、“./”或“../”开头。 ”
我有这个结构: www/templates/node_modules/www/templates/assets/calendar.js
这是我的 calendar.js 文件的开始:
import {Calendar} from '../node_modules/@fullcalendar/core/main.js';
import dayGridPlugin from '../node_modules/@fullcalendar/daygrid/main.js';
import timeGridPlugin from '../node_modules/@fullcalendar/timegrid/main.js';
import listPlugin from '../node_modules/@fullcalendar/list/main.js';
other code
Run Code Online (Sandbox Code Playgroud)
在导入 {Calendar} 的那一刻,它抛出错误。在@fullcalendar/core/main.js 我看到:
import './vdom';
import { __extends, __assign } from 'tslib';
import { render, createElement, CalendarRoot, CustomContentRenderContext, CalendarContent, flushToDom, DelayedRunner, CalendarDataManager, isArraysEqual, applyStyleProp, CalendarApi } from '@fullcalendar/common';
export * from '@fullcalendar/common';
Run Code Online (Sandbox Code Playgroud)
但我认为 tslib(和其他导入库)的方式是不正确的。但是 tslib 在目录 www/templates/node_modules/tslib 中。我不想编辑已安装的包(我认为这不是正确的方法)。我可以在 npm config 中设置路径吗?有可能的?或者最好的方法是什么?
谢谢。
我想知道 node_modules 文件夹到底是什么以及用于什么。我知道当我们使用 npm 下载任何库时,该库会转到 node_modules,但我知道当我们要将其上传到 github 时,我们必须忽略 node_modules 文件夹,因为它占用大量空间。通过 package.json 我们可以使用 npm i 下载所有依赖项,我的问题是...假设我想将我的应用程序/网站部署到某个服务器/主机,我是否也必须将 node_modules 文件夹上传到服务器?另一件事,通常我从网站下载我的 jquery 和 bootstrap 并复制到我的项目内的 css/js 文件夹中,但是这次我尝试使用 npm 并且一切都转到 node_modules 并且我使用的是cordova,当我执行命令cordova时build 既不生成我的 jquery 也不生成我的引导程序。所以这就是我的问题,如果我想主持我的项目,我真的必须上传 node_modules 吗?当它是cordova或ionic时,我还必须将node_modules复制到www文件夹吗?如果是,那么使用 npm 下载库有什么意义?这是真的吗?哪一个更好?去网站下载文件并粘贴到 www 或通过 npm 下载?
我想创建一个反应项目,我已经跑npx create-react-app my-app那么cd my-app,但当我有 npm start
这个错误已经表明:
> my-app@0.1.0 start C:\Users\USER\my-app
> react-scripts start
i ?wds?: Project is running at http://192.168.137.1/
i ?wds?: webpack output is served from
i ?wds?: Content not from webpack is served from C:\Users\HAMOOD\my-app\public
i ?wds?: 404s will fallback to /
Starting the development server...
events.js:292
throw er; // Unhandled 'error' event
^
Error: spawn powershell ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance …Run Code Online (Sandbox Code Playgroud) 所以我一直在尝试运行这个网络应用程序,起初它显示
(node:12960) 警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名。
C:\Users\J\react-messenger\stream-chat-boilerplate-api\src\index.js:1
从 'dotenv' 导入 dotenv; ^^^^^^
语法错误:不能在模块外使用导入语句
然后我去把 set the type: module 放在 package.json 中,但它给了我这个错误
参考错误:模块未定义
at file:///C:/Users/J/react-messenger/stream-chat-boilerplate-api/src/index.js:38:1
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import dotenv from 'dotenv';
dotenv.config();
import fs from 'fs';
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import compression from 'compression';
const api = express();
api.use(cors());
api.use(compression());
api.use(helmet());
api.use(bodyParser.urlencoded({ extended: true }));
api.use(bodyParser.json());
api.listen(process.env.PORT, error => { …Run Code Online (Sandbox Code Playgroud) 我以这种方式连接到 MQTT:
//mqtt.js
const mqtt = require('mqtt');
var options = {
//needed options
};
var client = mqtt.connect('mqtt://someURL', options);
client.on('connect', () => {
console.log('Connected to MQTT server');
});
Run Code Online (Sandbox Code Playgroud)
我想以这种方式导出客户端对象:
//mqtt.js
module.exports = client;
Run Code Online (Sandbox Code Playgroud)
这样我就可以将它导入其他文件并以这种方式使用它:
//anotherFile.js
const client = require('./mqtt');
client.publish(...)
Run Code Online (Sandbox Code Playgroud)
但是,我们都知道这是行不通的!我怎样才能做到这一点?
更新
我尝试了promise并得到了一个非常奇怪的行为。当我像下面的代码一样在同一个文件 (mqtt.js) 中使用 promise 时,一切正常:
//mqtt.js
const mqtt = require('mqtt');
var mqttPromise = new Promise(function (resolve, reject) {
var options = {
//needed options
};
var client = mqtt.connect('mqtt://someURL', options);
client.on('connect', () => { …Run Code Online (Sandbox Code Playgroud) 当我的脚本导入nodejieba时,我遇到了这个错误。
\n未找到名为 \xe3\x80\x8cModule:无法解析“C:\\Users\\user\\Documents\\cchatbigdata\\node_modules@mapbox\\node-pre-gyp\”中的“aws-sdk” \lib\\util'\xe3\x80\x8d
\n起初,反应运行良好,我的脚本工作正常。
\n然后我npm install nodejieba --save-dev \n并在 TestedData.js 中添加import nodejieba from 'nodejieba' 。
\n即使我不使用nodejieba的功能,我的反应也遇到了这个错误。
\n我开始搜索有关 无法解析'aws-sdk'的相关问题。
\n大多数人在使用webpack时遇到这个错误,但我不使用webpack,我只是导入nodejieba。
\n我尝试了解node_modules下的node-pre-gyp\\lib\\util。
\n但它是由React创建的(我使用npx create-react-app),所以我认为如果更改文件react可能会关闭。
\n我认为react和nodejieba之间有问题。
\n我的浏览器显示这些错误,我发现它们都与node-pre-gyp\\lib\\util有关。
./node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js\nModule not found: Can't resolve 'aws-sdk' in 'C:\\Users\\kevin\\Documents\\cchatbigdata\\node_modules\\@mapbox\\node-pre-gyp\\lib\\util'\nconsole.<computed> @ index.js:1\nhandleErrors @ webpackHotDevClient.js:174\npush../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage @ webpackHotDevClient.js:213\nindex.js:1 \n./node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js\nModule not found: Can't resolve 'mock-aws-s3' in 'C:\\Users\\kevin\\Documents\\cchatbigdata\\node_modules\\@mapbox\\node-pre-gyp\\lib\\util'\nconsole.<computed> @ index.js:1\nhandleErrors @ webpackHotDevClient.js:174\npush../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage @ webpackHotDevClient.js:213\nindex.js:1 \n./node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js\nModule not found: Can't resolve 'nock' in 'C:\\Users\\kevin\\Documents\\cchatbigdata\\node_modules\\@mapbox\\node-pre-gyp\\lib\\util'\nconsole.<computed> @ index.js:1\nhandleErrors @ webpackHotDevClient.js:174\npush../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage @ webpackHotDevClient.js:213\nRun Code Online (Sandbox Code Playgroud)\n也许必须修复node_modules下的node-pre-gyp。
\n这是我的 TestData.js
import {useState, useEffect} from …Run Code Online (Sandbox Code Playgroud) 我正在尝试进行简单的用户登录,并且想将哈希值和盐存储在 PostgreSQL 数据库中。
const {Client} = require("pg");
const bcrypt = require('bcrypt');
const client = new Client({
user:"xxxxxxx",
host:"xxxxxxxxxxxxxxxxxx",
database:"xxxxxxxxx",
password:"xxxxxxxxxxxxxxxxxxxxxxx",
port: 1234
});
client.connect()
const saltRounds: number = 10;
const newUserHash = (username: String, password: String) => {
bcrypt.genSalt(saltRounds, function(err: Error, salt: String) {
bcrypt.hash(password, salt, function(err: Error, hash: String) {
//send it to database
});
});
}
Run Code Online (Sandbox Code Playgroud)
但我不断收到此错误;Module not found: Can't resolve 'pg-native'。使用import pg from 'pg'对我来说不起作用,我不知道该怎么办了。
node-modules ×10
node.js ×6
reactjs ×4
javascript ×3
npm ×3
bootstrap-4 ×1
cmd ×1
database ×1
export ×1
fullcalendar ×1
jquery ×1
mqtt ×1
next.js ×1
npm-start ×1
postgresql ×1
syntax-error ×1
webpack ×1